2011-07-21 60 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

相關問題