将按钮标题传递给新视图 [英] Passing button title to a new view
本文介绍了将按钮标题传递给新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在视图上有一个按钮,按下该按钮可打开另一个视图.我想在第二个视图中设置一个标签以根据按下的按钮的标签进行更改.我试过在我的 switchViews 函数中传递 (id) 发件人,但这不起作用.请帮忙!
I have a button on a view which when pressed opens another second view. I want to set a label in the second view to change depending on the label of the button that was pressed. I have tried passing (id) sender in my switchViews function but that does not work. Please help!
推荐答案
传递发送者应该可以工作.只需将 sender 转换为 UIButton * 并使用 titleForState: 获取状态的标题.这是工作代码:
Passing sender should work. Just cast sender to UIButton * and take title for the state with titleForState:. Here is working code:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window makeKeyAndVisible];
UIButton * myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(100, 100, 100,80);
[myButton setTitle:@"test title" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(myButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[window addSubview:myButton];
}
- (void)myButtonClicked:(id)sender{
UIButton * clickedButton = (UIButton *)sender;
NSString * buttonTitle = [clickedButton titleForState:UIControlStateNormal];
NSLog(@"title: %@",buttonTitle);
UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 100,80)];
myLabel.text = buttonTitle;
[window addSubview:myLabel];
}
这篇关于将按钮标题传递给新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文