以编程方式切换 UIButton 上的 UILabel 文本

发布于 2024-12-01 20:24:09 字数 531 浏览 2 评论 0原文

当按下按钮时,我创建了以下插座

IBOutlet UIButton *oneButton;

和以下方法:

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = sender.titleLabel.text;
    if ([digit isEqualToString:@"1"])
    {
        oneButton.titleLabel.text = @"11";
    } else if ([digit isEqualToString:@"11"])
    {
        oneButton.titleLabel.text = @"1";
    }
}

所以基本上我想在按下按钮时在 1 和 11 之间切换按钮的标题。通过设置断点,我看到程序执行了 oneButton.titleLabel.text = @"11"; 行。但标题实际上从未改变。 oneButton 连接到 IB 中的文件所有者。

I created the following outlet

IBOutlet UIButton *oneButton;

and the following method when a button is pressed:

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = sender.titleLabel.text;
    if ([digit isEqualToString:@"1"])
    {
        oneButton.titleLabel.text = @"11";
    } else if ([digit isEqualToString:@"11"])
    {
        oneButton.titleLabel.text = @"1";
    }
}

So basically I want to toggle the title of the button between 1 and 11, whenever pressed. By putting breakpoint, I see that the program goes through the line oneButton.titleLabel.text = @"11"; but the title never actually changes. oneButton is connected to the file's owner in the IB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

孤千羽 2024-12-08 20:24:09

尝试这样的事情:

[oneButton setTitle:@"11" forState:UIControlStateNormal];

Try something like this instead:

[oneButton setTitle:@"11" forState:UIControlStateNormal];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文