Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine viewWillAppear from Popped UINavigationController or UITabBarController

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

like image 743
DogCoffee Avatar asked Jul 20 '15 02:07

DogCoffee


People also ask

When viewWillAppear is called?

viewwillappear method is called as and when the view controller's view is added to the window. ( if the view is already in the window and is hidden by another view, this method is called when the view is once again revealed). The method is a notification to the view controller that the view is about to become visible.

What is the difference between viewDidLoad and viewWillAppear?

The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.

Which is called first viewDidLoad or viewDidAppear?

viewDidLoad is called once when the controller is created and viewDidAppear is called each time the view, well, DID appear. So say you have a modal view that you present, when that view is dismissed, viewDidAppear will be called, and viewDidLoad will not be called.

Why does viewWillAppear not get called when an app comes back from the background?

The Simple Answer The technical reason for when viewWillAppear gets called is simple. Notifies the view controller that its view is about to be added to a view hierarchy. It can't be any view hierarchy — it has to be the one with a UIWindow at the root (not necessarily the visible window).


1 Answers

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
    }
}
like image 84
JAL Avatar answered Nov 15 '22 03:11

JAL