2番目のView Controllerでプロトコルとデリゲートを定義します
@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];
}
最初のView Controllerでプロトコルを実装します
@interface FirstViewController : UIViewController <SecondViewControllerDelegate>
2 番目のビュー コントローラーをプッシュするときは、最初のビュー コントローラーを 2 番目のデリゲートとして設定します。
- (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
}