如何在 iOS 中以编程方式设置按钮文本?
我知道如何在 iOS 中以编程方式更改按钮文本:
[myButton setTitle:@"myTitle" forState:UIControlStateNormal];
但我的需要是我有一个名为 _lblgenisis 的标签,我放置了一些代码来更改此标签的文本,它对我来说效果很好,代码是:
_lblGenisis.text = [NSString stringWithFormat:@"%@ %@",delegate.selectedBook,delegate.selectedChapter];
我的要求是我必须转换为此代码以在按钮中显示相同的内容而不是标签,我的按钮名称是 _btnGenisis。如何做到这一点?
解决方案答案:
对我有用的
NSString *myTitle = [NSString stringWithFormat:@"%@ %@",delegate.selectedBook,delegate.selectedChapter];
[nextButton setTitle:myTitle forState:UIControlStateNormal];
I know how to change the button text programmatically in iOS:
[myButton setTitle:@"myTitle" forState:UIControlStateNormal];
but my need is I have a label called _lblgenisis, I put some code to change the text of this label and it works fine for me, the code for that is:
_lblGenisis.text = [NSString stringWithFormat:@"%@ %@",delegate.selectedBook,delegate.selectedChapter];
my requirement is I have to convert to this code to show the same in button instead of label my button name is _btnGenisis. How to do this?
Solution
Answer that works for me:
NSString *myTitle = [NSString stringWithFormat:@"%@ %@",delegate.selectedBook,delegate.selectedChapter];
[nextButton setTitle:myTitle forState:UIControlStateNormal];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我看看我是否做对了。
通常用于更改按钮标签的代码无法以编程方式设置它?
我遇到过这个问题,事实证明:
当您在 xib 文件中设置标签时,您可以看到结果并且不会出现任何错误。最近,我有一个带有图像的自定义按钮,但不明白为什么无法以编程方式添加标题。
事实证明,我正确设置了标题(您已经知道如何设置)。真正的问题是我使用
setImage:forState
设置了按钮的图像。该图像位于按钮上方,您应该检查 xib 文件以查看是否为该按钮设置了图像。如果是,请删除该图像并将其设置为背景图像。要查看这是否确实是问题所在,请在您尝试设置标题的代码中尝试此操作,
它应该输出 (null)
如果它输出就像您有一个图像覆盖了按钮的标题。
检查接口文件并检查按钮。确保没有默认图像集。您可以设置默认背景图像。
如果它都是在代码中创建的,请搜索
[_btnGenisis setImage:
如果您想要标题,则不应设置图像。使用
Let me see if I get this right.
The code that normally works for changing your button label is not working to set it programmatically?
I have had this issue it turns out that:
When you set the label in the xib file you can see the results and won't make any mistakes. Recently I had a custom button with an image and couldn't figure out why I could not add a title programmatically.
It turns out I was setting the title properly (the way you already know how to). The real issue was that I set my button's image with
setImage:forState
. This image is above the button and you should check your xib file to see if you have an image set for the button. If so, delete the image and set it as the background image.To see if this is actually the issue, try this in your code where you are trying to set the title
It should output (null)
If it outputs like you have an image covering your button's title.
Check the interface file and inspect the button. Make sure there is no default image set. You can set a default background image.
If it is all created in code do a search for
[_btnGenisis setImage:
You should not be setting the image if you want a title. Use
尝试将标签添加到按钮上?
[_btnGenisis addSubview:_lblGenisis]
Try to add the label to the button?
[_btnGenisis addSubview:_lblGenisis]