Search code examples
iosobjective-cios7viewdidloadviewdidappear

Method called only in viewDidApper but not in viewDidLoad, why?


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
}

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!