在xib中我的UIButtons是怎么回事? [英] What's going on with my UIButtons in a xib?

查看:93
本文介绍了在xib中我的UIButtons是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组UIButtons(在xib中定义),它们有需要定期更新的标签。在这些按钮的superview的视图控制器的ViewDidLoad方法中,我有一个更新方法,对每个按钮:

  button.titleLabel.text = @相关文字; 
[button setNeedsDisplay];

,当你点击一个按钮,另一个方法运行,弹出一个UIAlertView,在视图控制器上的方法与初始文本设置方法完全相同:

  button.titleLabel.text = @ 新文本; 
[button setNeedsDisplay];

然而,此代码根本不工作,按钮标签的文本不会更新方法,它仍然是一个空白的白色按钮。在xib中我没有在按钮上定义任何文本 - 没有点,按钮文本没有意义,除非它在运行时设置。无论如何,在一个漂亮的,我决定设置其中一个按钮的文本测试测试。



现在,当我点击那个特定的按钮,它弹出启动UIAlertView,但在后台将按钮的文本更改为测试测试测试测试。和 时间,UIAlertView回调执行我期望它,并设置只有该按钮的文本。当我再次点击它,文本回到测试测试测试,直到我关闭UIAlertView,它将再次运行回调方法,并将按钮文本设置为任何方法应该。 p>

我不知道这里发生了什么,或者为什么设置文本最初在xib有任何关系是否我可以设置该文本后编程。显然这不是我想要的行为,我想知道如何肯定设置按钮上的文本。



编辑:SVD关于setTitle的建议:ForState:解决我的问题,谢谢。我仍然很好奇为什么标题设置在.xib为什么显示,但是只有当我有一个UIAlertView弹出。

解决方案

您可能需要使用[setTitle:forState:]来设置正常和突出显示(或选中)状态的按钮标题。



确保按钮已连接到插座,如jtbandes指出)。


I have a set of UIButtons (defined in a xib) who have labels that need to be updated periodically. In the ViewDidLoad method of the view controller of those buttons' superview, I have an update method that does, for each button:

button.titleLabel.text = @"Relevant Text";
[button setNeedsDisplay];

and when you tap a button, another method runs which pops up a UIAlertView, which in turn calls back a method on the view controller which does much the same thing as the initial text setting method:

button.titleLabel.text = @"New Text";
[button setNeedsDisplay];

however, this code simply isn't working, the button label's text doesn't get updated in either method, it remains a blank white button. In the xib I don't define any text on the buttons - there's no point, the button text doesn't make sense unless it's set at runtime. Anyway, on a lark, I decided to set the text of one of the buttons to "test test test".

Now, when I tap that particular button, it pops up the UIAlertView but in the background changes the text of the button to "test test test test". And this time, the UIAlertView callback does what I expect it to and sets the text for only that button. When I hit it again, the text goes back to "test test test test" until I dismiss the UIAlertView, which again will run the callback method and set the button text to whatever the method should.

I have no idea what's going on here, or why setting the text initially in the xib has any relation to whether or not I can set that text later programatically. Obviously this isn't the behavior I want, I want to know how to for sure set the text on the buttons.

Edit: SVD's advice about setTitle:ForState: solved my problem, thanks. I'm still curious though as to why the title label set in the .xib shows up, but only when I have a UIAlertView pop up.

解决方案

You may need to use [setTitle: forState:] to set the button title for normal and highlighted (or selected) state.

(And do make sure the button is connected to the outlet, as jtbandes points out).

这篇关于在xib中我的UIButtons是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆