1. 保证第一vc 有navigation controller 并有segue,也就两个vc之间有连线
**modal方式
***第二个vc上需要有返回按钮,
返回代码为
- (IBAction)tappedCloseModal:(id)sender {
[self dismissModalViewControllerAnimated: YES];
}
**push方式
***back按钮自动生成
*若vc点击按钮关闭vc,
代码如下;
- (IBAction)tappedCloseModal:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
2. 保证第一vc 有navigation controller,但两个vc之间无连线: 代码形式
**push方式
***第一个vc上有导航按钮
***第二个vc的有storyboardID
导航按钮代码
- (IBAction)tappedNavButton:(id)sender {
SecondViewController *secondViewController =
[self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
[self.navigationController pushViewController:secondViewController animated:YES];
}
**modal 方式
- (IBAction)tappedShowNewView:(id)sender {
SecondViewController *secondViewController =
[self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
[self presentViewController:secondViewController animated:YES completion:nil]
}
3. 代理方式
**首先给第二个vc添加protocol 和delegate
@class SecondViewController;
@protocol SecondViewControllerDelegate <NSObject>
- (void)doSomethingWithSecondViewController:(SecondViewController *)secondViewController;
@end
@interface SecondViewController : UIViewController
@property (nonatomic, weak) id <SecondViewControllerDelegate> delegate;
- (IBAction)tappedCloseModal:(id)sender;
@end
**使用代理delegate
@implementation SecondViewController
@synthesize delegate;
- (IBAction)tappedCloseModal:(id)sender {
[delegate doSomethingWithSecondViewController:self];
//如果不写 @synthesize delegate; 就要用 self.delegate
}
**使用protocol
首先在第一个vc中加入跳转方法
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface ViewController : UIViewController <SecondViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *lblInfo;
@property (weak, nonatomic) IBOutlet UITextField *txtText;
- (IBAction)tappedClickMe:(id)sender;
- (IBAction)tappedNavButton:(id)sender;
- (IBAction)tappedShowNewView:(id)sender;
@end
***初始化显示第二个vc,设置代理(第一个vc中)
- (IBAction)tappedShowNewView:(id)sender {
SecondViewController *secondViewController =
[self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
//什么时候发送代理权
secondViewController.delegate = self;
[self presentViewController:secondViewController animated:YES];
}
***实现方法(第二个vc中)--就是第二个页面要做的关闭操作。
- (void)doSomethingWithSecondViewController:(SecondViewController *)secondViewController {
[self dismissModalViewControllerAnimated:YES];
}
如果第一个有navigation
[self.navigationController popViewControllerAnimated:YES];