更新titleLabel的UIButton
我有一个UIButton有titleLabel,例如,像这样:@"Download it!"
更新titleLabel的UIButton
我想我的下载完成后,更新titleLabel我与另一个文本按钮,例如,像这样的: @"Already downloaded!"
我可以改变状态(启用或不启用),但不可能刷新/更新UIButton的titleLabel。
任何想法如何做到这一点?我试过[myButton setNeedsDisplay];
但它不起作用。
感谢您的建议和帮助。
更新1: 解决方案:
[yourButton setTitle:<#(NSString *)#> forState:<#(UIControlState)#>]
你试过吗?
[yourButton setTitle:<#(NSString *)#> forState:<#(UIControlState)#>]
您可以更改按钮标题标签中的文本。
[aButton setTitle:@"Already downloaded!" forState:UIControlStateNormal];
有关主题的更多信息,以及控制状态的完整列表,请参阅本:https://developer.apple.com/library/ios/documentation/uikit/reference/uicontrol_class/reference/reference.html#//apple_ref/doc/c_ref/UIControlState
所有的例子与这篇文章解释说明的标题按钮的各种状态,像UIControlStateNormal
的变化,UIControlStateHighlighted
但它不会在下载完成时完成。
最简单的方法是保持通知您的viewController
某个进程(下载)完成。然后,根据需要更改按钮标题。
可以试试这段代码。
-
在你的ViewController
viewDidLoad
添加一个按钮&一个通知观察者self.someButton.title = @"Download Now"; // set the button title // Add notification Observer [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyDownloadComplete:) name:@"DOWNLOAD_COMPLETE" object:nil];
-
现在定义观察的目标方法执行按钮标题更改为
-(void)notifyDownloadComplete:(NSNotification*)note { self.someButton.title = @"Already Downloaded"; }
-
现在通过GCD添加一个下载方法&然后发布通知一旦完成。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Here your non-main thread. Try Downloading something dispatch_async(dispatch_get_main_queue(), ^{ //Here you returns to main thread. [[NSNotificationCenter defaultCenter] postNotificationName:@"DOWNLOAD_COMPLETE" object:nil]; }); });
这会的self.someButton
标题更改为任何你想要的,因为在这种情况下Already Downloaded
。
希望有所帮助。
您是否正在使用[button setTitle: forState:]来更新它? – tanzolone
+1,谢谢大家! – Lapinou
@Lapinou。一个建议。如果你有问题的答案,最好接受一个答案(任何一个在这里)..这样的问题不会在“未回答的问题”..希望你明白.. –