在点击事件中设置 UIButton 标题的问题

发布于 2024-11-18 11:37:34 字数 204 浏览 2 评论 0原文

当用户使用以下代码按下按钮时,我试图更改按钮的标题:

- (IBAction) hideKB: (UIButton *) sender {
     sender.titleLabel.text = @"↓";
}

但是当我单击应用程序时,应用程序崩溃了,我不明白为什么。

删除发送者的东西,按钮工作没有问题。

I'm trying to change the title of a button when the user press on it with the following code:

- (IBAction) hideKB: (UIButton *) sender {
     sender.titleLabel.text = @"↓";
}

But when I click the app crashes and I can't understand why.

Removing the sender stuff, the button works with no problems.

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

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

发布评论

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

评论(3

悲凉≈ 2024-11-25 11:37:34

操作的正确方法签名是

- (IBAction)action:(id)sender

您的应用程序正在崩溃,因为您的对象正在发送一条它不理解的消息。

尝试将您的代码替换为以下内容:

- (IBAction)hideKB:(id)sender {
    UIButton *button = (UIButton *)sender;
    [button setTitle:@"↓" forState:UIControlStateNormal];
}

正如您可能注意到的那样,我还更改了设置按钮标题的行。这是因为您永远不应该直接操作 UIButtontitleLabel 属性,而应该使用适当的 setter 方法,如上所示。

编辑:澄清一下,大多数控件都允许您使用点表示法来编辑其 titleLabeltext 属性。但是,UIButton 实例支持不同的标题(以及图像和背景图像),具体取决于它们所处的状态。

如果您想知道为什么 UIButton 可能处于几种不同的状态之一,您经常看到的一个很好的例子是“灰色”的按钮。这意味着这些按钮处于 UIControlStateDisabled 状态。您可以找到控件所有可能状态的列表 文档中

The correct method signature for an action is

- (IBAction)action:(id)sender

Your app is crashing because your object is being sent a message it doesn't understand.

Try replacing your code with something along the lines of:

- (IBAction)hideKB:(id)sender {
    UIButton *button = (UIButton *)sender;
    [button setTitle:@"↓" forState:UIControlStateNormal];
}

As you may notice I've also changed the line that sets the button title. This is because you should never manipulate a UIButton's titleLabel property directly, but rather you should be using the appropriate setter method as shown above.

Edit: To clarify, most controls will allow you to use dot notation to edit the text property of their titleLabel. However, UIButton instances support different titles (as well as images and background images) depending on the state they're in.

If you're wondering why a UIButton could be in one of several different states, a good example you often see is buttons that are "greyed out". What this means is that those buttons are in the UIControlStateDisabled state. You can find a list of all the possible states for a control in the documentation.

关于从前 2024-11-25 11:37:34

尝试使用

 - (void)setTitle:(NSString *)title forState:(UIControlState)state

例如

 [sender setTitle:"↓" forState:UIControlStateNormal];

Try using

 - (void)setTitle:(NSString *)title forState:(UIControlState)state

e.g.

 [sender setTitle:"↓" forState:UIControlStateNormal];
焚却相思 2024-11-25 11:37:34

您可以尝试:

[(UIButton *)sender setTitle: @"↓"" forState: UIControlStateNormal];

有关更多信息,您可以参考 setTitle:forState: 在 UIButton 类参考中

希望这会有所帮助。

you can try with:

[(UIButton *)sender setTitle: @"↓"" forState: UIControlStateNormal];

For more information, you can refer to setTitle:forState: in UIButton Class Reference

Hope this helps.

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