Gchammas23
Gchammas23

Reputation: 142

How to disable Tool bar item from a different view controller in Xcode

So I created a multi view app that uses a toolbar item that allows the user to navigate through the app. This toolbar item is used in all three of the views I have for the application. The main problem I am facing is that I want the user to no longer be able to tap the toolbar item when he opens the second view to allow the view to show all its contents (Since I am gradually showing labels). I just want to know if I am able to disable the toolbar item from the controller of the second view.

The toolbar item is linked to the ViewController.m. And I want to be able to disable it from the PCRViewController.m controller.

Note that I have already tried creating a method in the ViewController.m and calling it in the second controller, I have also tried to use the following line of code in the PCRViewController.m self.navigationItem.rightBarButtonItem.enabled = NO; but both did not do the job.

Here's an image of how I have the toolbar placedToolbar picture

If there's anything else you need me to provide please tell me so I can fix this problem. Thank you for your help!

PS: I used the following method to switch from one view to another:

- (IBAction)switchViews:(id)sender{
    if (!self.questionsViewController.view.superview){
        if (!self.questionsViewController){
            self.questionsViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Questions"];
        }
    }
    
    if (!self.pcrViewController.view.superview){
        if (!self.pcrViewController){
            self.pcrViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PCR"];
        }
    }
    
    if (!self.infoViewController.view.superview){
        if (!self.infoViewController){
            self.infoViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Info"];
        }
    }
    
    //Switch views
    if (self.questionsViewController.view.superview){
        self.pcrViewController.view.frame = self.view.frame;
        [self switchViewFromViewController:self.questionsViewController toViewController:self.pcrViewController];
    }
    else if (self.pcrViewController.view.superview){
        self.infoViewController.view.frame = self.view.frame;
        [self switchViewFromViewController:self.pcrViewController toViewController:self.infoViewController];
    }
    else{
        self.questionsViewController.view.frame = self.view.frame;
        [self switchViewFromViewController:self.infoViewController toViewController:self.questionsViewController];
    }
}

Below is the code of the switchViewFromViewController:toViewController method I have used:

- (void)switchViewFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    if (fromVC != nil) {
        [fromVC willMoveToParentViewController:nil];
        [fromVC.view removeFromSuperview];
        [fromVC removeFromParentViewController];
    }
    if (toVC != nil) {
        [self addChildViewController:toVC];
        [self.view insertSubview:toVC.view atIndex:0];
        [toVC didMoveToParentViewController:self];
    }
    
}

Upvotes: 0

Views: 129

Answers (1)

Hitesh patel
Hitesh patel

Reputation: 48

You can user NSnotificationcentre to communicate with the other conroller and then disable it

Upvotes: 0

Related Questions