一:从起始的控件做一条联线到目标场景。目标场景是navigationcontroller的话用model,否则用push。
二:将这个联线制定一个独特的Identifier。
三:为目标场景制作一个代理方法。
//声明一个协议
@class DetailsViewController;
@protocol DetailsViewControllerDelegate <NSObject>
- (void)DetailsViewControllerDidCancel: //协议的方法
(DetailsViewController *)controller;
- (void)DetailsViewControllerDidSave:
(DetailsViewController *)controller;
@end
@interface DetailsViewController : UITableViewController
@property (nonatomic, weak) id <DetailsViewControllerDelegate> delegate; //声明一个该协议的对象
- (IBAction)cancel:(id)sender;
- (IBAction)done:(id)sender;
@end
别忘了 @synthesize delegate;
四:在Cancel和Done按钮,以及所有其他你需要和原始场景交流的地方调用代理方法。
- (IBAction)cancel:(id)sender
{
[self.delegate DetailsViewControllerDidCancel:self];
}
- (IBAction)done:(id)sender
{
[self.delegate DetailsViewControllerDidSave:self];
}
五:在原始场景执行代理方法,这将会在用户按下按钮后关闭场景。
@interface TableViewController : UITableViewController
<DetailsViewControllerDelegate>
//协议方法的实现
- (void)DetailsViewControllerDidCancel:
(DetailsViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)DetailsViewControllerDidSave:
(DetailsViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
六:在原始场景执行prepareForSegue方法
//这个是如果目标场景是navigationcontroller的话,其他的照改
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
- {
- if ([segue.identifier isEqualToString:@"AddPlayer"])
- {
- UINavigationController *navigationController =
- segue.destinationViewController;
- PlayerDetailsViewController
- *playerDetailsViewController =
- [[navigationController viewControllers]
- objectAtIndex:0];
- playerDetailsViewController.delegate = self;
- }
- }