我会在第二个视图控制器中定义一个协议和委托
@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
}