当按钮被点击我将在第二视图控制器定义一个协议&委托
@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