pixelbitlabs
pixelbitlabs

Reputation: 1934

How can I hide a button in a different view controller?

I am using this code to hide a button in a different view controller, but the button does not get hidden when the button is pressed to hide the button in the other view controller.

This is the code I am using to hide the button in the other view controller:

    [self dismissModalViewControllerAnimated:YES];

    NSLog(@"Exited");

    ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

    [vc.mainbutton1 setHidden:YES];

Why is this not working?

Thanks!

Upvotes: 2

Views: 2957

Answers (5)

It seems you want a certain button to be hidden if something has been happening somewhere else.

You COULD, somewhat as a hack (but I don't mind that very much) control this with a variable on your AppDelegate for instance.

When the "something" is happening "somewhere else", do this:

MyAppDelegate *appDelegate = [[(MyAppDelegate *)UIApplication sharedApplication] delegate];
appDelegate.shouldHideThatOtherButtonLater = YES;

Then, when you create your new ViewController later on you could use this value to determine if your button should be visible or not like this:

ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

MyAppDelegate *appDelegate = [[(MyAppDelegate *)UIApplication sharedApplication] delegate];
[vc.mainbutton1 setHidden: appDelegate.shouldHideThatOtherButtonLater ];

You will in this case have to prepare your AppDelegate for this by creating and synthesizing that shouldHideThatOtherButtonLater-property.

Upvotes: 0

Tendulkar
Tendulkar

Reputation: 5540

take a BOOL variable in ViewController controller and make the property and synthesize also. and do this.

ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
vc.check = YES;

in the view controller viewdidload write this

if(self.check)
    [mainbutton1 set hidden:YES];

Upvotes: 4

Paul.s
Paul.s

Reputation: 38728

The other answers should work unless...

Judging by your code I am going to guess that you are trying to hide a button on the viewController that presented the modal view?

If this is correct then what you are doing will not work as you are creating a new instance of ViewController which is not the already existing viewController you want to use.

Although the docs say that it is fine to call [self dismissModalViewControllerAnimated:YES]; from the presented modal view I tend to set up a delegate to handle the dismissal like in Apple's utitliy app template.

Upvotes: 1

jbat100
jbat100

Reputation: 16827

From your question it sounds like you want to hide the button in an existing view controller, whereas in your code you are creating a new one

ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[vc.mainbutton1 setHidden:YES];

Either the view controller which you observe is not the one you expect or the mainbutton1 outlet is not connected properly. You can check if the memory controller is the one you expect by logging its memory address.

NSLog(@"Hid button for view controller %p", vc);

And doing the same in the viewDidAppear callback of ViewController

NSLog(@"In viewDidAppear for view controller %p", self);

Upvotes: 0

Michael Dautermann
Michael Dautermann

Reputation: 89559

The reason this isn't working is because even though you have alloc'd and init'd the ViewController properly, the actual elements of that vc ViewController (including mainbutton1) have not been loaded yet.

Hitman has the right idea (and I'm voting his idea up).

Either put in a BOOL property for setting mainButton1 to hidden when the view appears, or call your [mainButton1 setHidden: YES] right after you explicitly display the view (via animation or adding subviews or whatever).

Upvotes: 0

Related Questions