Accessing item of a navigationController childview in swift

256 views Asked by At

newbie to ios app coding here so any help would greatly be appreciated.

I have an app that when rotated to landscape opens a side menu automatically and I need it to disable the menu button in the child view of the navigationController. Here's my code.

Navigation controller

import Foundation
class GDNavigationController:UINavigationController{
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //Send notification when the device is rotated.
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
    }

    func rotated(){
        /*
        Put the code here to access the menu button
        */
        if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
        {
            //disable the menu button here
            self.revealViewController().setFrontViewPosition(FrontViewPosition.Right, animated: false)
        }

        if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
        {
            //enable the menu button here
            self.revealViewController().setFrontViewPosition(FrontViewPosition.Left, animated: false)
        }
    }
}

My ViewController code

import Foundation
class LocalCollectionController: UICollectionViewController{      
    @IBOutlet var MenuViewButton: UIBarButtonItem!
    override func viewDidLoad() {
        MenuViewButton.target = self.revealViewController()
        MenuViewButton.action = Selector("revealToggle:")
    }  
}

I have different navigationControllers load with different viewControllers based on which menu item is selected. The different navigationControllers share the same subclass but I never know which viewController is loaded, this is what I need to find out and how to access the button in that viewController.

Can anyone help?

1

There are 1 answers

0
Al Martin On BEST ANSWER

So I figured it out, change my navigation controller to this

import Foundation class GDNavigationController:UINavigationController{

var BBItem = UIBarButtonItem()
override func viewDidAppear(animated: Bool) {
    BBItem = self.topViewController.navigationItem.leftBarButtonItem!

}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    //Send notification when the device is rotated.
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
    self.rotated()
}


func rotated(){

    if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
    {
        self.revealViewController().setFrontViewPosition(FrontViewPosition.Right, animated: true)
        self.BBItem.enabled = false
    }

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
    {
        self.revealViewController().setFrontViewPosition(FrontViewPosition.Left, animated: true)
        self.BBItem.enabled = true
    }

}

}