前提是我们已经已经有了一个根UIViewController
我们新建一个SecondUIViewController 并且有一个sttr的属性 便于存储第一个界面传给第二个界面的值
在RootUiViewController中写入这些东西
//如果有多个按钮要触发同一个操作,但是又想实现不同按钮方法将执行不同操作时,就要判断一下是哪个按钮按下了
- (void)click:(UIButton *)button{
if (button.tag == 2) {
NSLog(@"button2 点我了");
}else if (button.tag == 3){
//新建一个界面
WJJSecondViewController * second = [[WJJSecondViewController alloc] init];
//给第二个界面传参数
second.attr = @"根据属性传值";
//设置反转动画
second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//跳转
[self presentViewController:second animated:YES completion:nil];
}
}
接下来,到SecondUIViewController.m中
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
[self createButton];
}
- (void)createButton{
UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
btn1.frame = CGRectMake(50, 50, 50, 50);
btn1.backgroundColor = [UIColor blackColor];
[btn1 setTitle:@"返回" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//得到前面一个界面传过来的值
NSLog(@"%@",self.attr);
[self.view addSubview:btn1];
}
- (void)back{
//返回到前一个界面 此方法与前一个界面跳转的方法是一对,有它必有前面那个
//[self presentViewController:second animated:YES completion:nil];
//并且动画跟前一个界面转过来时得动画一样
[self dismissViewControllerAnimated:YES completion:nil];
}
效果图如下