我在视图上有一个按钮,当按下该按钮时会打开另一个第二个视图。我想在第二个视图中设置一个标签,以根据按下的按钮的标签进行更改。我试过在我的switchViews函数中传递(id)发送者,但这不起作用。请帮忙!
答案 0 :(得分:0)
在第二个视图控制器上,创建UILabel作为属性,即
@interface MyViewcontroller : UIViewController {
UILabel *titleLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *titleLabel;
并在界面构建器中将其附加到要更改的标签上。
然后,在第一个视图控制器的switchViews方法中,在创建第二个视图后,您可以设置如下标题:
...
MyViewController *newViewController = [[MyViewController alloc] initWithNibName:'something' bundle:nil];
newViewController.view.titleLabel.text = @"Your new title goes here";
...
希望有所帮助。
答案 1 :(得分:0)
传递发件人应该有效。只需将发件人转换为UIButton *并使用titleForState获取状态的标题:这是工作代码:
- (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];
}