Jon
Jon

Reputation: 4732

How To Change UIButton Of Same UIViewController?

I have a a UIViewController that is pushed to by two different views in my app.

One time it is a modal view, so I have the right navbar button set to Done and it dismisses the view.

At another time in my app, this same view is pushed to, but not modally, thus I don't want this button to show. I tried adding this when pushing it, but no luck.

self.navigationItem.rightBarButtonItem.enabled = NO;

Upvotes: 0

Views: 102

Answers (2)

Legolas
Legolas

Reputation: 12325

Simple and effective -

self.navigationItem.rightBarButtonItem = nil;

Edit:

How can you add this when you are pushing this ? Add it in the viewWillAppear or viewDidLoad of the viewController you want to see this is in.

You can check for a certain condition.

  1. If it is pushed from view 1, you can make it nil.
  2. If it is shown modally from view 2, you can make it appear.

For this, you will have to make the viewControllers communicate with each other. For that, you will need to use NSUserDefaults and set an integer for a key.

You can assign two different integers logically and use them as the condition for showing/not showing the rightBarButtonItem.

Good Luck.

Upvotes: 1

Sam
Sam

Reputation: 3659

You can check the parent view controller for whether it has the modalViewController property set

if (self.parentViewController.modalViewController == self)
{
    // add button
}

Upvotes: 2

Related Questions