在点击事件中设置 UIButton 标题的问题
当用户使用以下代码按下按钮时,我试图更改按钮的标题:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
操作的正确方法签名是
您的应用程序正在崩溃,因为您的对象正在发送一条它不理解的消息。
尝试将您的代码替换为以下内容:
正如您可能注意到的那样,我还更改了设置按钮标题的行。这是因为您永远不应该直接操作
UIButton
的titleLabel
属性,而应该使用适当的 setter 方法,如上所示。编辑:澄清一下,大多数控件都允许您使用点表示法来编辑其
titleLabel
的text
属性。但是,UIButton
实例支持不同的标题(以及图像和背景图像),具体取决于它们所处的状态。如果您想知道为什么
UIButton
可能处于几种不同的状态之一,您经常看到的一个很好的例子是“灰色”的按钮。这意味着这些按钮处于 UIControlStateDisabled 状态。您可以找到控件所有可能状态的列表 文档中。The correct method signature for an action is
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:
As you may notice I've also changed the line that sets the button title. This is because you should never manipulate a
UIButton
'stitleLabel
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 theirtitleLabel
. 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 theUIControlStateDisabled
state. You can find a list of all the possible states for a control in the documentation.尝试使用
例如
Try using
e.g.
您可以尝试:
有关更多信息,您可以参考 setTitle:forState: 在 UIButton 类参考中
希望这会有所帮助。
you can try with:
For more information, you can refer to setTitle:forState: in UIButton Class Reference
Hope this helps.