0

I'm calling a method from another controller, but for some reason it does not work. The NSLog works fine but myButton is not showing up.

First controller .h:

-(void) buttonChange;

First controller .m

-(void)buttonChange {
    myButton.hidden=NO; //this is not getting executed
    NSLog(@"it's working");
}

- (void)viewDidLoad{
    myButton.hidden=YES; //initially hidden
   //....other codes
}

Second controller:

FirstController *theButtonInstance = [[FirstController alloc] init];
[theButtonInstance buttonChange]; //all works fine when I call this, but button is not showing up
angular_learner
  • 671
  • 2
  • 14
  • 31

4 Answers4

0

Place this line myButton.hidden=YES; in init method of FirstController.

SGDev
  • 2,256
  • 2
  • 13
  • 29
0

You're creating your instance of FirstController, but viewDidLoad hasn't run yet. Then immediately, you're setting hidden to NO. Later, viewDidLoad will be called, and it will set hidden back to YES before the view appears. You have to wait until viewDidLoad is called before you can correctly set hidden, and it won't get called right at the creation of the controller. You could do something like:

[self performSelector:@selector(buttonChange) withObject:nil afterDelay:3.0] ;

To see your button not appear then appear 3 seconds later.

Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35
0

If you want to create a new instance of your FirstController (what you currently do like Undo already mentioned) you don´t have to call [theButtonInstance buttonChange]; but set theButtonInstance.myButton.hidden = NO; after you created the new instance of FirstController

LoVo
  • 1,856
  • 19
  • 21
0

You can create a BOOL property in FirstViewController.h. And set that property to NO before pushing the controller. After that go to the viewDidLoad method in FirstViewController and write the if condition whether to hide or show the button based on that property. Hope that works for you. Let me know if you need code for this.

Jassi
  • 537
  • 8
  • 21