最新修改(2012-11-17):两个segue都为Push,指向同一个View Controller,通过变量指定是新增一个对象还是修改原来对象。
============================================================================
只是为了试验下,所以代码和界面做地比较搓。
通过点击1,进行一个Model 的 Segue,显示2;
通过点击2的返回按钮,进行一个Push 的Segue,显示3。
如图1
图1
类似于通讯录新增联系人。
通过点击2-1中的+,显示2-2新增联系人并进行编辑;
通过点击2-2的Done,显示2-3进行编辑或者返回所有联系人。
如图2
图2
简单代码实现:
ModalViewController.h
1 #import <UIKit/UIKit.h> 2 3 @protocol ModalVCDelegate <NSObject> 4 5 - (void)gob; 6 7 @end 8 9 @interface ModalViewController : UIViewController 10 11 @property (strong, nonatomic) id <ModalVCDelegate> delegate; 12 13 - (IBAction)goback:(id)sender; 14 15 @end
ModalViewController.m
1 #import "ModalViewController.h" 2 3 @interface ModalViewController () 4 5 @end 6 7 @implementation ModalViewController 8 9 @synthesize delegate; 10 11 - (IBAction)goback:(id)sender { 12 [self.delegate gob]; 13 } 14 15 @end
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 5 - (void)show; 6 7 @end
ViewController.m
1 #import "ViewController.h" 2 #import "ModalViewController.h" 3 4 @interface ViewController () <ModalVCDelegate> 5 6 @end 7 8 @implementation ViewController 9 10 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 11 if ([segue.identifier isEqualToString:@"segueModal"]) { 12 ModalViewController *c = segue.destinationViewController; 13 c.delegate = self; 14 } 15 } 16 17 - (void)gob { 18 [self show]; 19 [self dismissViewControllerAnimated:YES completion:NULL]; 20 } 21 22 - (void)show { 23 [self performSegueWithIdentifier:@"seguePush" sender:nil]; 24 } 25 26 @end
PushViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface PushViewController : UIViewController 4 5 @end
PushViewController.m
1 #import "PushViewController.h" 2 3 @interface PushViewController () 4 5 @end 6 7 @implementation PushViewController 8 9 @end