Lapinou
Lapinou

Reputation: 1477

Update titleLabel UIButton

I have a UIButton with a titleLabel, for example, like this: @"Download it!"

I would like after my download is finished, update the titleLabel of my button with another text, for example, like this: @"Already downloaded!"

I can change the state (enable or not) but impossible to refresh / update the titleLabel of the UIButton.

Any idea how to do this ? I tried [myButton setNeedsDisplay]; but it doesn't work.

Thank for your suggestions and help.

UPDATE 1: Solution:

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

Upvotes: 1

Views: 319

Answers (3)

Balram Tiwari
Balram Tiwari

Reputation: 5667

All the example explained with this post explains the change of title on button's various state like UIControlStateNormal, UIControlStateHighlightedbut it is not doing it on download complete.

The simplest way is to keep notify your viewController that the some process (the download) is finished. Then you change the button Title as required.

May be try this code.

  1. Add a button & a Notification observer in your ViewController viewDidLoad as

    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. Now define the target method of the Observer to perform the Button title Change as

    -(void)notifyDownloadComplete:(NSNotification*)note {
        self.someButton.title = @"Already Downloaded";
    }
    
  3. Now add a Download Method via GCD & then post the notification once it is completed.

    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];
           });
       });
    

This will change the title of self.someButton to whatever you want, as in this case as Already Downloaded.

Hope that helps.

Upvotes: 2

Mick MacCallum
Mick MacCallum

Reputation: 130222

You can change the text in a button's title label.

[aButton setTitle:@"Already downloaded!" forState:UIControlStateNormal];

For more information on the topic, and the full list of control states, see this: https://developer.apple.com/library/ios/documentation/uikit/reference/uicontrol_class/reference/reference.html#//apple_ref/doc/c_ref/UIControlState

Upvotes: 2

Prince Agrawal
Prince Agrawal

Reputation: 3607

Have you tried this?

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

Upvotes: 5

Related Questions