UILabel的文字不会改变
大家好,我正在另一个按钮内以编程方式创建一个 UILabel 和一个 UIButton,因此当按下此按钮时,它会创建一个 UILabel 和一个 UIButton,并将其添加为单独页面上的子视图。我想要一个能够将标签值加 1 的按钮。以下是将标签添加到第二个视图的按钮的代码:
-(IBAction)outlet1:(id)sender{
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[addButton addTarget:self action:@selector(addButton1:)
forControlEvents:UIControlEventTouchUpInside];
[addButton setTitle:@"+" forState:UIControlStateNormal];
addButton.frame = CGRectMake(80.0, 210.0, 35, 40.0);
[cart.view addSubview:addButton];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(137, 150, 25, 35)];
label.backgroundColor = [UIColor clearColor];
label.text = @"1";
[cart.view addSubview:label];
}
这是我希望能够更改标签的按钮:
- (void)addButton1:(id)sender {
[black.label setText:[NSString stringWithFormat:@"%d",[black.label.text intValue] +1]];
}
black
是与第一个 viewController 关联的 ivar, cart
与第二个 viewController 关联!我的问题是,当我按下与 addButton1:
方法连接的按钮时,它崩溃了?预先感谢您的帮助!
Hey guys i am creating a UILabel and a UIButton programatically inside another button, so when this button is pressed, it creates a UILabel and a UIButton and adds is as a subview on a seperate page. I want a button to be able to add 1 to the labels value. Here is the code for the button that adds the label to the second view:
-(IBAction)outlet1:(id)sender{
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[addButton addTarget:self action:@selector(addButton1:)
forControlEvents:UIControlEventTouchUpInside];
[addButton setTitle:@"+" forState:UIControlStateNormal];
addButton.frame = CGRectMake(80.0, 210.0, 35, 40.0);
[cart.view addSubview:addButton];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(137, 150, 25, 35)];
label.backgroundColor = [UIColor clearColor];
label.text = @"1";
[cart.view addSubview:label];
}
and this is the button i want to be able to alter the label:
- (void)addButton1:(id)sender {
[black.label setText:[NSString stringWithFormat:@"%d",[black.label.text intValue] +1]];
}
black
is an ivar that is associated with the first viewController and cart
is assoctiated with the second viewController!My problem is when i press the button that is hooked up with the addButton1:
method, it crashes? Thanks in advance for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在
viewDidLoad
中初始化您的UILabel
并为其创建一个 ivar,然后您可以在整个@implementation.
You need to init your
UILabel
inviewDidLoad
and create an ivar to it, then you can access it throughout your@implementation
.