开发中遇到一个需求:点击 button,跳转到下一个页面,下个页面要获取从哪个 button点击过来的,这就需要在触发 button的点击事件时获取button的文字。代码如下:
在button的触发事件里
//寻物
- (IBAction)lostBtn:(id)sender {
SudoVController *ctl = [[SudoVController alloc]init];
//获取button的title并赋值给string类型的buttonTitle
NSString *buttonTitle = _lostBtn.titleLabel.text;
ctl.title = buttonTitle; //给title赋值,就是要跳转到页面的title
[self.navigationController pushViewController:ctl animated:YES];
}
//招领
- (IBAction)foundBtn:(id)sender {
SudoVController *ctl = [[SudoVController alloc]init];
NSString *buttonTitle = _foundBtn.titleLabel.text;
ctl.title = buttonTitle;
[self.navigationController pushViewController:ctl animated:YES];
}