當按鈕被點擊我將在第二視圖控制器定義一個協議&委託
@protocol SecondViewController;
@interface SecondViewController : UIViewController
@property (nonatomic, assign) id<SecondViewController> delegate;
@end
@protocol SecondViewController <NSObject>
- (void)secondViewController:(SecondViewController *)controller didTappedOnButton:(UIButton *)button;
@end
然後調用該委託:
- (IBAction)buttonTapped:(UIButton *)sender
{
// do somthing..
// then tell the delegate about the button tapped
[self.delegate secondViewController:self didTappedOnButton:sender];
}
在您的第一視圖控制器實現協議
@interface FirstViewController : UIViewController <SecondViewControllerDelegate>
當您按下第二個視圖控制器時,設置第一作爲第二委託:
- (void)someMethodThatPushTheSecondViewController
{
SecondViewController *svc = [[SecondViewController alloc] init];
[self.navigationController pushViewController:svc animated:YES];
svc.delegate = self;
}
並實現委託方法時,按鍵敲擊
- (void)secondViewController:(SecondViewController *)controller didTappedOnButton:(UIButton *)button
{
// do somthing after button tapped
// you can get the button title from button.titleLabel.text
}
你無法得到通知。改爲使用'viewWillAppear'方法。 – Martol1ni 2012-08-10 10:57:07
即使在viewWillAppear中,您也無法執行它,而無需執行協議 – 2012-08-10 12:53:10
@TeodorCarstea雖然這基本上是真的,但主要的回收信息是,當活動視圖控制器要觸發視圖中已收到'viewDidDisappear ',無論您用於更新的機制是什麼,都不應直接更新非活動視圖(該視圖可能在發生'didReceiveMemoryWarning'時發佈)。您必須更新您的模型,並且只有當另一個視圖控制器變爲活動狀態並且接收到'viewWillAppear'時,才應該執行UI更改。 – Rob 2012-08-11 01:11:47