2012-03-11 92 views
2

呼叫協議方法我是相當新的對象 - 和學習有關使用協議和委託。具有多個子視圖層次

當我只有兩個視圖時,我沒有遇到下面的示例來實現協議/傳遞數據,但是,當我有幾個子視圖時,當我嘗試調用某個方法時,出現「無法識別的選擇器」錯誤。

例如,在這樣一個場景,我有

  • FirstViewController
  • SecondViewController
  • ThirdViewController

我想ThirdViewController回調到FirstViewController。

通用代碼會是這樣的:在

FirstViewController.h

在firstViewController.m

//present a second controller which will control settings for the app 
SecondViewController *secondViewController = [[SecondViewController alloc]  initWithNibName:@"secondViewController" bundle:nil]; 

secondViewController.delegate= self; 

secondViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 

[self presentModalViewController: secondViewController animated: YES]; 

@interface FirstViewController : UIViewController <MyProtocol> 

後來

-(void) aMethod{ 
    //carry out some action here 
} 
在secondView

Controller.m或者

//present a third controller...maybe a table view for selecting music 
ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"thirdViewController" bundle:nil]; 

thirdViewController.delegate= self; 

thirdViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 

[self presentModalViewController: thirdViewController animated: YES]; 

在ThirdViewContoller.h

//Create a protocol to implement options back on the firstViewController 
@protocol MyProtocol; 

@interface thirdViewController 
{ 
    IBOutlet UIButton *aButton; 
} 

@property (nonatomic, weak) id<MyProtocol> delegate; 
-(IBAction) callMethod:(id)sender; 
@end 

@protocol MyProtocol <NSObject> 
- (void) aMethod; 
@end 
在ThirdViewController.m

@synthesize delegate; 

-(IBAction) callMethod:(id)sender{ 
    [self.delegate aMethod]; 
} 

運行時出現該消息將只發送回secondViewController,而不是到firstViewController因爲錯誤是:

- [SecondViewControlle r aMethod:]:無法識別的選擇器發送到實例0x19d620

我認爲有一些基本的概念,設置尚未學習,或程序結構錯誤的代理插座。

有隻用兩個,在這裏工作得很好鑑於代碼的例子不勝枚舉,但我還沒有發現在多個視圖很多信息。如果我的程序設計真的不正確,我很抱歉。

回答

1

您需要SecondViewController符合您的協議:

@interface SecondViewController : UIViewController <MyProtocol> 

你試圖調用,只有在第一個存在的第二個視圖控制器上的方法。如果你想傳達回第一視圖控制器,那麼你就必須定義一個第二協議來做到這一點。

這是一個非常好的學術活動,瞭解功能和協議的限制,但你也應該注意到命名衝突。在命名協議時儘可能描述性。理想情況下,你必須對一組頭文件是這樣的:

@interface FirstViewController : UIViewController <SecondViewControllerDelegate> 
@end 

而第二個視圖控制器:

@protocol SecondViewControllerDelegate <NSObject> 
-(void)someSecondViewControllerDelegateMethod; 
@end 

@interface SecondViewController : UIViewController <ThirdViewControllerDelegate> 

@protocol (nonatomic, weak) id <SecondViewControllerDelegate> delegate; 

@end 

最後,第三個視圖控制器:

@protocol ThirdViewControllerDelegate <NSObject> 
-(void)someThirdViewControllerDelegateMethod; 
@end 

@interface ThirdViewController : UIViewController 

@protocol (nonatomic, weak) id <ThirdViewControllerDelegate> delegate; 

@end 

所以第二個視圖控制器可以實現-(void)someThirdViewControllerDelegateMethod,它可能看起來像:

-(void)someThirdViewControllerDelegateMethod 
{ 
    [self.delegate someFirstViewControllerDelegateMethod]; 
} 

這就是第三個視圖控制器可以如何回調到第一個;它有點級聯並傳遞消息。

+0

它的工作原理!謝謝!!! – ssnytt 2012-03-14 00:17:21

+0

我很高興這爲你工作!如果您發現我的答案令人滿意,請考慮將答案標記爲「已接受」。 – 2012-03-14 15:30:16