2011-06-24 56 views
0

基本上,我想调用主类持有指针的子类中的主类的函数。
我知道我可以得到主类来启动子类并将函数附加到它。但我不希望这样,我希望子类使用其init函数启动自己。
有什么办法可以从someClass调用doSomething?
一些基本的代码,以显示我想要的东西:
在MainViewController.h:如何从类内的对象中调用类函数?

@class SomeClass; 

@interface MainViewController : UIViewController { 
     SomeObject *someInformation; 
     SomeClass *someInstance; 
} 
@property (nonatomic, strong) SomeClass *someInstance; 

-(void)doSomething:(id)sender; 
@end 

在MainViewController.m:

@implementation MainViewController 
@synthesize someInstance; 
-(void)doSomething:(id)sender { 
    //do something to someInformation 
} 
@end 

在SomeClass.h:

@interface SomeClass : NSObject { 
    UIStuff *outstuff; 
} 
@property (strong, nonatomic) UIStuff *outstuff; 
-(void)somethingHappened:(id)sender; 
@end 

SomeClass.m

@implementation SomeClass 
@synthesize outStuff; 
-(IBAction)somethingHappened:(id)sender { 
    //call doSomething to the main class that points to this class 
} 
@end 
+0

你能解释一下吗?你想从什么课程中调用什么功能? –

+0

我想从SomeClass调用doSomething。 (补充说,对这个问题) –

回答

2

您的术语不稳定。类不“保留”其他类。在你的情况下,类MainViewController的实例有指针到类SomeClass的对象。我不迂腐;如此糟糕的术语会让人怀疑自己对基础和重要概念的理解。

也就是说,如果您希望SomeClass对象能够将消息发送到MainViewController实例,SomeClass对象需要对MainViewController对象的引用。从您发布的代码中,不存在这样的参考。您需要扩展SomeClass接口以存储对MainViewController对象的明确引用,或者您可以通过委派使用稍微更加间接的(至少在概念上)的东西。但是,由于您未提供特定案例的信息,因此我们的解决方案将形成缺乏详细信息的洞察。

+0

没错,我不太清楚确切的术语。我会尽量修复它。我想我正在寻找的是你提到的有关参考的后退。你能不能解释一下如何创建这个参考,以及我能用它做什么? –

+0

这样做的主要原因是清理我的MainViewController文件。我不想在一个庞大的课堂上完成所有的入门和功能,而是想根据他们的工作将他们分成更小的课程。 –

+0

Coleman建议存储引用后,我设法想出了一种解决此问题的方法: Add to SomeClass.h @property(strong,nonatomic)MainViewController * mainView; - (void)setMainView:(MainViewController *)thisMainView; 添加到SomeClass.m @synthesize MAINVIEW - (无效)setMainView:(MainViewController *)thisMainView { MAINVIEW = thisMainView; } 在主类中调用setMainView 那么你可以在某些类中使用[mainView doSomething] 我在这里发布,因为我不能自己回答。 –

相关问题