您的位置:首页 > 产品设计 > UI/UE

Snail—UI学习之UIButton实现界面跳转及属性传值

2015-07-22 20:25 661 查看
前提是我们已经已经有了一个根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];
}


效果图如下



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: