Determine viewWillAppear from Popped UINavigationController or UITabBarController
Asked Answered
B

3

6

I am unable to find a way to distinguish between popping from the Nav controller stack and entering the view controller from the UITabBarController.

I want to call a method in ViewWillAppear only when the view is presented from the TabBar, not when someone presses back in the navigation controller.

If I wasn't using a TabBarController, I could easily get this functionally using viewDidLoad.

I've tried,

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    println("View Will Appear")

    if isBeingPresented() {
        println("BP")
    }
    if isMovingFromParentViewController() {
        println("from")
    }
    if isMovingToParentViewController() {
        println("to")
    }
}

But there is no difference when I present from pressing the Tab Button or when press back button.

Only the "View Will Appear" is getting called.

Using iOS 8.4 / Swift

Big answered 20/7, 2015 at 2:6 Comment(4)
did you tried to add some observers and use NSNotificationCenter ?Snarl
didn't think of that - will have a thinkBig
how does your ViewController stack look like? Is your View Controller always contained in a Navigation controller (either as the root view controller or a pushed view controller)?Boresome
Yeah, UITabBarController, 4 tabs, each tab contains UINavControllers (that have UITableViewControllers that drill down)Big
C
4

Sounds like a good use of the UITabBarControllerDelegate.

First, add a Bool property on your ViewController comingFromTab:

class MyViewController: UIViewController {
    var comingFromTab = false
    
    // ...
}

Set your UITabBarControllerDelegate to whatever class you want and implement the method shouldSelectViewController. You may also want to subclass UITabBarController and put them in there.

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
    
    if let myViewController = viewController as? MyViewController {
        myViewController.comingFromTab = true
}

If your tab's initial view controller is a UINavigationController, you will have to unwrap that and access it's first view controller:

if let navController = viewController as? UINavigationController {
    if let myViewController = navController.viewControllers[0] as? MyViewController {
        // do stuff
    }
}

Lastly, add whatever functionality you need in viewWillAppear in your view controller:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    
    // ...
    if comingFromTab {
        // Do whatever you need to do here if coming from the tab selection
        comingFromTab = false
    }
}
Cirro answered 28/7, 2015 at 20:54 Comment(1)
I had to reverse the comingFromTab = false to true, as on first load it would not work on the first time that tab was selected. Swapping it, made it work 100%. I didn't want to use the TabBarDelegate but seems this is the only way.Big
E
5

There is no way to know for sure. So I guess the easiest way is to add some variable that you will have to change before popping back to that view controller and checking it's state in viewWillAppear.

class YourViewController: UIViewController {
    var poppingBack = false

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        if !poppingBack {
            // your logic
        }
        else {
            poppingBack = false // reset it for next time
        }
    }
}

// somewhere else in code, suppose yourVC is YourViewController
yourVC.poppingBack = true
self.navigationController.popToViewController(yourVC, animated: true)

You can also try implementing UINavigationControllerDelegate's - navigationController:willShowViewController:animated: method and check if it will be called when presenting your view controller from tab bar.

Egyptian answered 23/7, 2015 at 17:23 Comment(0)
C
4

Sounds like a good use of the UITabBarControllerDelegate.

First, add a Bool property on your ViewController comingFromTab:

class MyViewController: UIViewController {
    var comingFromTab = false
    
    // ...
}

Set your UITabBarControllerDelegate to whatever class you want and implement the method shouldSelectViewController. You may also want to subclass UITabBarController and put them in there.

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
    
    if let myViewController = viewController as? MyViewController {
        myViewController.comingFromTab = true
}

If your tab's initial view controller is a UINavigationController, you will have to unwrap that and access it's first view controller:

if let navController = viewController as? UINavigationController {
    if let myViewController = navController.viewControllers[0] as? MyViewController {
        // do stuff
    }
}

Lastly, add whatever functionality you need in viewWillAppear in your view controller:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    
    // ...
    if comingFromTab {
        // Do whatever you need to do here if coming from the tab selection
        comingFromTab = false
    }
}
Cirro answered 28/7, 2015 at 20:54 Comment(1)
I had to reverse the comingFromTab = false to true, as on first load it would not work on the first time that tab was selected. Swapping it, made it work 100%. I didn't want to use the TabBarDelegate but seems this is the only way.Big
S
0

You can check parentViewController property

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    if parentViewController is UITabBarController {
        // Presented by UITabBarController
    } else if parentViewController is UINavigationController {
        // Presented by UINavigationController
    } else {
        // Presented by ...
    }
}
Spelter answered 27/7, 2015 at 14:11 Comment(3)
Doesn't work - Presented by UITabBarController is never called. Either when I select my Tab, or when segue back in the Nav Controller stack. Only the is UINavigationController is called, and called on both occasions.Big
What structure do you have? I was thinking something like: UITabBarController with one page with custom vc and another page with UINavigationController which may push same custom vcSpelter
Yeah, UITabBarController, 4 tabs, each tab contains UINavControllersBig

© 2022 - 2024 — McMap. All rights reserved.