2011-07-21 38 views
0

在我的应用程序中有两个tabbars。在tab-0父类中是“FirstViewController”,而在Tab-1父类中是“SecondViewController”。在“SecondViewController”中,我声明了协议和自定义委托方法。我想通过“FirstViewController”(FVC)传递信息。因此FVC必须作为代表进行分配。如何为我的视图控制器实现自定义委托方法

现在我的疑问是,现在我在“SVC”。我如何将“FVC”分配为“SVC”的代表?

在 “SVC”

[[self delegate] sendCoordinates:self]; 

方法的定义是 “FVC”。要执行此方法,首先我需要将“FV​​C”指定为委托。

我希望我在解释我的问题时很清楚。

在此先感谢。

回答

0

您需要设置委托。让我证明:

`SVC.h` 

@interface SVC 
{ 
    id _delegate; 
} 

- (void)setDelegate:(id)delegate; //sets the delegate 
- (id)delegate; //gets the delegate 

@end 

@protocol SVCDelegate 

- (void)sendCoordinates:(SVC *)svc; 

@end 

然后,在SVC.m,你打电话委托在完全相同的方式,你在你的问题表明,这样[[self delegate] sendCoordinates:self];

现在,在FVC,你需要#import SVC.h和初始化对象。

SVC*svcObject = [[SVC alloc] init]; 
[svcObject setDelegate:self]; //self now refers to FVC - I think you're missing this one 
//work with the object and when you're done, get rid of it in dealloc: 
[svcObject setDelegate:nil]; 
[svcObject release]; 

在同一个类中,实现- (void)sendCoordinates:(SVC *)svc并设置委托后,它会被称为。

我想你错过了setDelegate:阶段,这就是为什么它不起作用。

希望它有帮助!

注意:在SVC中,请记住保留该委托,否则它将变为nil并且不会调用委托方法。不要忘记一旦完成就释放该代表。

+0

无论你解释的是完美的。但这里的情况有点棘手。要设置委托我必须移动到“FVC”比我只能设置委托。程序计数器现在在“SVC”中。 – Harsh

+0

移动FVC?你是什​​么意思? – Pripyat

+0

现在程序计数器(PC)在“SVC”中。我想通过“FVC”中的信息。为此,我必须设置“FVC”作为代表。代码将正常工作。但对于那台PC必须跳到“FVC”。它将如何发生? – Harsh

相关问题