UILabel的文字不会改变

发布于 2024-12-03 16:43:02 字数 1040 浏览 3 评论 0原文

大家好,我正在另一个按钮内以编程方式创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2024-12-10 16:43:02

您需要在 viewDidLoad 中初始化您的 UILabel 并为其创建一个 ivar,然后您可以在整个 @implementation.

@synthesize label;

- (void) viewDidLoad
{
    label = [[UILabel alloc] initWithFrame:CGRectMake(137, 150, 25, 35)];
}

You need to init your UILabel in viewDidLoad and create an ivar to it, then you can access it throughout your @implementation.

@synthesize label;

- (void) viewDidLoad
{
    label = [[UILabel alloc] initWithFrame:CGRectMake(137, 150, 25, 35)];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文