neu*_*ino 40 objective-c uibutton ios ios7 xcode5
UIButton
当用户点击它时,我正在尝试以编程方式更改我创建的标题.所以,这是我创建的代码UIButton
:
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
[myButton setBackgroundColor:[UIColor blackColor]];
[myButton setAlpha:0.7];
[myButton setTitle:@"Hello" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(userClicked:) forControlEvents:UIControlEventTouchUpInside];
[parentView addSubview:myButton];
Run Code Online (Sandbox Code Playgroud)
而且,在我的userClicked:
方法中,我做:
-(void) userClicked:(UIButton*)button
{
NSLog(@"USER CLICKED!!!");
if ([NSThread isMainThread])
{
NSLog(@"is main thread");
}
[button setTitle:@"Bye" forState:UIControlStateHighlighted];
[button setTitle:@"Bye" forState:UIControlStateNormal];
[button setTitle:@"Bye" forState:UIControlStateSelected];
[self someLengthyComputation];
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是我可以看到打印的日志消息:
USER CLICKED!!!
isMainThread
Run Code Online (Sandbox Code Playgroud)
但是,按钮的标题不会改变!我究竟做错了什么?
编辑:设置几个州的标题也不起作用.
EDIT2:如果我在Xcode的调试器窗口中打印按钮的描述,它会显示正确的标题!
Printing description of button->_titleView:
<UIButtonLabel: 0xa4c9310; frame = (95 216; 130 22); text = 'Bye'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xa44f080>>
Run Code Online (Sandbox Code Playgroud)
小智 48
这对我来说更新了标题文本(iOS 7.1,Xcode 5.1):
button.enabled = FALSE;
[button setTitle:@"Test" forState:UIControlStateNormal];
button.enabled = TRUE;
Run Code Online (Sandbox Code Playgroud)
Jef*_*Sun 24
我遇到了同样的问题,我注意到其他地方我都在设置attributesTitle.因此,对标题的任何更新都不会影响属性标题.
我的解决方案
[button setAttributedTitle:@"" forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
代替
[button setTitle:@"" forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
Luk*_*asz 20
目前,我参与的最短工作是:
[button setNeedsLayout];
Run Code Online (Sandbox Code Playgroud)
更新标题后.
这似乎发生在iOS 7.1中.在Xcode 5.1.1 SDK 7.1中编译时,所有在以前的iOS版本中正常运行的按钮(或者可能只是使用以前的SDK编译)突然停止这样做.
看起来像Apple的bug.
use*_*868 10
我使用故事板时遇到了类似的问题.使用上面的答案,我不得不使用
[mybutton setTitle:@"SomeText" forState:UIControlStateNormal];
[button setNeedsLayout];
[button layoutIfNeeded];
Run Code Online (Sandbox Code Playgroud)
我必须确保属性检查器中的按钮类型为"自定义"而不是"系统".
请查看这可能对您有所帮助......单击按钮时检查条件是否为buttonToggled ...如下所示,当您有像changeButtonText这样的函数时
-(IBAction)changeButtonText:(id)sender {
if (!buttonToggled) {
[sender setTitle:@"Initial Text" forState:UIControlStateNormal];
buttonToggled = YES;
} else {
[sender setTitle:@"Alternative Text" forState:UIControlStateNormal];
buttonToggled = NO;
}
}
Run Code Online (Sandbox Code Playgroud)
您的代码中存在几个问题:
您正在为该按钮分配回调:
@selector(userClicked:)
Run Code Online (Sandbox Code Playgroud)
但是你的代码是另一种方法:
-(void)userTapOnTapToRefreshView:(UIButton*)button
Run Code Online (Sandbox Code Playgroud)
要解决这个问题,你需要实现这样的东西:
-(void)userClicked:(id)sender
{
[(UIButton*)sender setTitle:@"Bye" forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)
这部分代码对我来说也没有意义:
[parentView myButton];
Run Code Online (Sandbox Code Playgroud)
尝试将其更改为:
[parentView addSubview:myButton];
Run Code Online (Sandbox Code Playgroud)
希望它会有所帮助:)
对于Swift 3到5,只需使用以下内容:
button.setTitle("My title", for: .normal)
Run Code Online (Sandbox Code Playgroud)
或者对于属性文本使用这个:
button.setAttributedTitle(<AttributedString>, for: .normal)
Run Code Online (Sandbox Code Playgroud)
这有点晚了,有点与 Walter Schurter 的回应有关:
我有一个类似的问题,在我更新到 7.1 之前,我的按钮文本设置正确。然后我发现由于我的按钮被禁用,我必须为禁用状态设置标题颜色和标题文本才能显示文本。然后一切都像魅力一样!
在iOS 7中,UIButton的标题在禁用时不会更新.这似乎是iOS 7上的一个错误.
作为解决方法,更新正常和禁用标题.有用!
[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateDisabled];
Run Code Online (Sandbox Code Playgroud)
尝试这个:
[button setTitle:@"Bye" forState:UIControlStateHighlighted];
Run Code Online (Sandbox Code Playgroud)
或者:
[button setTitle:@"Bye" forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)
您还可以修改您的 void 函数:
-(void) userClicked
{
NSLog(@"USER CLICKED!!!");
if ([NSThread isMainThread])
{
NSLog(@"is main thread");
}
[myButton setTitle:@"Bye" forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
37867 次 |
最近记录: |