0

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
}
Gualty
  • 251
  • 1
  • 4
  • 13
  • 1
    Don't forget to call `[super viewDidAppear:animated];` in your `viewDidAppear:` method. – rmaddy Mar 20 '14 at 16:43
  • I added [super viewDidAppear:animated]; but when I first open the viewController the number 1 appers for less than a second and the disappear. If I change viewController and than turn back the number 1 appears normally and did not disappear – Gualty Mar 20 '14 at 16:45
  • Then you've got something else going on. – Aaron Mar 20 '14 at 16:48
  • Add a breakpoint and follow the code through to see what method is called when. Also, if `labelRequestNumber` is a property I would recommend you use the accessor methods (i.e. access the property using `self.` rather than the ivar directly). – Robotic Cat Mar 20 '14 at 16:54
  • Same results If i use self instead of _labelRequestNumber – Gualty Mar 20 '14 at 16:59

2 Answers2

0

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!

meda
  • 45,103
  • 14
  • 92
  • 122
  • @Gualty I suggest you to add some logs such as `NSLog(@"viewWillAppear called!!!");` – meda Mar 20 '14 at 16:53
  • I added the Log function and when I open for the first time the view It seems to load both viewDidAppear and viewWillApper. Every time the text appear for less than a second and then disappear. – Gualty Mar 20 '14 at 16:56
  • The log shows but then disappear? call it from only `viewWillAppear`, you can comment out the others – meda Mar 20 '14 at 16:59
  • No disappear the text of the label after a second but if I change the view and turn back it does not disappear – Gualty Mar 20 '14 at 17:00
  • I see that you hardcoded `numeroRichieste` do you expect the text to be 1 in red all the time? – meda Mar 20 '14 at 17:03
0

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.

Greg
  • 25,317
  • 6
  • 53
  • 62
  • Tried to add the viewWillApper method but the problem persists – Gualty Mar 20 '14 at 16:49
  • Sorry it should be viewDidAppear, my mistake. Add it to the beginning on viewDidAppear method. – Greg Mar 20 '14 at 16:52
  • My Code now is: -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self controlloRichieste]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self controlloRichieste]; } – Gualty Mar 20 '14 at 16:54
  • You don't need viewWillAppeare method. – Greg Mar 20 '14 at 16:55
  • Same results if I remove viewWillApper – Gualty Mar 20 '14 at 16:57
  • Can you put NSLog to viewDidAppeare and see is it called every time? I believe the issue is in your controlloRichieste method. The viewDidAppeare is called every time the view appears on the screen. – Greg Mar 20 '14 at 17:00
  • Yes, the viewDidApper is called every time I open the view. – Gualty Mar 20 '14 at 17:02
  • In the line where you compare string you use isEqual, you should use isEqualToString instead. – Greg Mar 20 '14 at 17:05
  • What do you supposed to achieve? Do you want to open another view controller, update the numeroRichieste and see the effect when you go back to the main view controller? – Greg Mar 20 '14 at 17:10
  • The value of the label Will be updated from the internet in the future. So every time it must check the value and show the number of the request – Gualty Mar 20 '14 at 17:26
  • Your code is fine. In controlloRichieste method you assign 1 to the variable: NSString *numeroRichieste = @"1"; and after that you do if check. Every time the open the view the else statement will be called because the numeroRichieste = @"1" is equal 1. What do you expect? – Greg Mar 20 '14 at 17:29
  • I set the variable to 1 to make some test, in the future that value will be fetched from the internet. – Gualty Mar 21 '14 at 09:35