2014-01-21 50 views
1

我有一個UIButton有titleLabel,例如,像這樣:@"Download it!"更新titleLabel的UIButton

我想我的下載完成後,更新titleLabel我與另一個文本按鈕,例如,像這樣的: @"Already downloaded!"

我可以改變狀態(啓用或不啓用),但不可能刷新/更新UIButton的titleLabel。

任何想法如何做到這一點?我試過[myButton setNeedsDisplay];但它不起作用。

感謝您的建議和幫助。

更新1: 解決方案:

[yourButton setTitle:<#(NSString *)#> forState:<#(UIControlState)#>] 
+1

您是否正在使用[button setTitle:<#(NSString *)#> forState:<#(UIControlState)#>]來更新它? – tanzolone

+0

+1,謝謝大家! – Lapinou

+1

@Lapinou。一個建議。如果你有問題的答案,最好接受一個答案(任何一個在這裏)..這樣的問題不會在「未回答的問題」..希望你明白.. –

回答

5

你試過嗎?

[yourButton setTitle:<#(NSString *)#> forState:<#(UIControlState)#>] 
2

所有的例子與這篇文章解釋說明的標題按鈕的各種狀態,像UIControlStateNormal的變化,UIControlStateHighlighted但它不會在下載完成時完成。

最簡單的方法是保持通知您的viewController某個進程(下載)完成。然後,根據需要更改按鈕標題。

可以試試這段代碼。

  1. 在你的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]; 
    
  2. 現在定義觀察的目標方法執行按鈕標題更改爲

    -(void)notifyDownloadComplete:(NSNotification*)note { 
        self.someButton.title = @"Already Downloaded"; 
    } 
    
  3. 現在通過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

希望有所幫助。

相關問題