Question

I created a method that sets the title of a button based on a value.

This method needs to be called when opening the viewController and maybe refreshed when the controller appears again.

So i created the method and I called that method in viewDidLoad and viewDidApper but it seems to be called only when I change page and turn back to the view controller.

Why?

My code is

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self controlloRichieste];

......
}

-(void)viewDidAppear:(BOOL)animated{

[self controlloRichieste];
}


-(void)controlloRichieste{
//Numero richieste di contatto
NSString *numeroRichieste = @"1";

if([numeroRichieste isEqual:@"0"]){
    [_labelRequestNumber setTitle:@"Nessuna" forState:UIControlStateNormal];
} else {
    _labelRequestNumber.titleLabel.text = numeroRichieste;
    _labelRequestNumber.tintColor = [UIColor redColor];
}
//Fine Numero richieste di contatto
}
Was it helpful?

Solution

You can also move that code to viewWillAppear so that it gets called each time it appears.

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];

  [self controlloRichieste];
}

I see the problem now, try the other way around

-(void)controlloRichieste{
    //Numero richieste di contatto
    NSString *numeroRichieste = @"1";

    if([numeroRichieste isEqual:@"0"]){
        [_labelRequestNumber setTitle:@"Nessuna" forState:UIControlStateNormal];
    } else {
        _labelRequestNumber.tintColor = [UIColor redColor];
        [[_labelRequestNumber titleLabel]setText:numeroRichieste];
    }
    //Fine Numero richieste di contatto
}

Change set the button color, before you change its titleLabel's text


I created a demo PROJECT for you, hope it's helpful!

OTHER TIPS

When you open view first time the viewDidLoad is called and the viewDidAppeare.

The viewDidAppeare is called every time when the view is opened, when you push or present other view controller and go back to the maine one viewDidAppeare is called. You should call:

[super viewDidAppear:animated];

The viewDidLoad is called just when the view is loaded and after that when it's deallocated and it needs to be allocated again. So mostly when you push or present other view controller and go back to the maine one viewDidLoad is not called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top