从弹出的 UINavigationController 或 UITabBarController 确定 viewWillAppear

Determine viewWillAppear from Popped UINavigationController or UITabBarController(从弹出的 UINavigationController 或 UITabBarController 确定 viewWillAppear)

本文介绍了从弹出的 UINavigationController 或 UITabBarController 确定 viewWillAppear的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到一种方法来区分从导航控制器堆栈弹出和从 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.

我只想在视图从 TabBar 呈现时调用 ViewWillAppear 中的方法,而不是当有人在导航控制器中按下回时.

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.

如果我不使用 TabBarController,我可以使用 viewDidLoad 轻松获得此功能.

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

我试过了,

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

    println("View Will Appear")

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

但是当我按下 Tab 按钮或按下返回按钮时没有区别.

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.

使用 iOS 8.4/Swift

Using iOS 8.4 / Swift

推荐答案

听起来很好用 UITabBarControllerDelegate.

Sounds like a good use of the UITabBarControllerDelegate.

首先,在 ViewController comingFromTab 上添加一个 Bool 属性:

First, add a Bool property on your ViewController comingFromTab:

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

将您的 UITabBarControllerDelegate 设置为您想要的任何类并实现方法 shouldSelectViewController.您可能还想继承 UITabBarController 并将它们放在那里.

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
}

如果您的选项卡的初始视图控制器是 UINavigationController,您将必须打开它并访问它的第一个视图控制器:

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
    }
}

最后,在视图控制器的 viewWillAppear 中添加您需要的任何功能:

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
    }
}

这篇关于从弹出的 UINavigationController 或 UITabBarController 确定 viewWillAppear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从弹出的 UINavigationController 或 UITabBarController 确定 viewWillAppear

基础教程推荐