<i id='5UK5w'><tr id='5UK5w'><dt id='5UK5w'><q id='5UK5w'><span id='5UK5w'><b id='5UK5w'><form id='5UK5w'><ins id='5UK5w'></ins><ul id='5UK5w'></ul><sub id='5UK5w'></sub></form><legend id='5UK5w'></legend><bdo id='5UK5w'><pre id='5UK5w'><center id='5UK5w'></center></pre></bdo></b><th id='5UK5w'></th></span></q></dt></tr></i><div id='5UK5w'><tfoot id='5UK5w'></tfoot><dl id='5UK5w'><fieldset id='5UK5w'></fieldset></dl></div>

  1. <small id='5UK5w'></small><noframes id='5UK5w'>

  2. <tfoot id='5UK5w'></tfoot>

    • <bdo id='5UK5w'></bdo><ul id='5UK5w'></ul>
    <legend id='5UK5w'><style id='5UK5w'><dir id='5UK5w'><q id='5UK5w'></q></dir></style></legend>

      从弹出的 UINavigationController 或 UITabBarController 确定 viewWill

      时间:2023-06-12

          <tfoot id='PzERP'></tfoot>
            <bdo id='PzERP'></bdo><ul id='PzERP'></ul>
                <legend id='PzERP'><style id='PzERP'><dir id='PzERP'><q id='PzERP'></q></dir></style></legend>
              • <small id='PzERP'></small><noframes id='PzERP'>

              • <i id='PzERP'><tr id='PzERP'><dt id='PzERP'><q id='PzERP'><span id='PzERP'><b id='PzERP'><form id='PzERP'><ins id='PzERP'></ins><ul id='PzERP'></ul><sub id='PzERP'></sub></form><legend id='PzERP'></legend><bdo id='PzERP'><pre id='PzERP'><center id='PzERP'></center></pre></bdo></b><th id='PzERP'></th></span></q></dt></tr></i><div id='PzERP'><tfoot id='PzERP'></tfoot><dl id='PzERP'><fieldset id='PzERP'></fieldset></dl></div>
                  <tbody id='PzERP'></tbody>
                本文介绍了从弹出的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:iOS 5 [UINavigationBar 外观] setBackgroundImage 不起作用 下一篇:结合导航控制器和标签栏控制器

                相关文章

                1. <small id='lxgIz'></small><noframes id='lxgIz'>

                  • <bdo id='lxgIz'></bdo><ul id='lxgIz'></ul>
                2. <i id='lxgIz'><tr id='lxgIz'><dt id='lxgIz'><q id='lxgIz'><span id='lxgIz'><b id='lxgIz'><form id='lxgIz'><ins id='lxgIz'></ins><ul id='lxgIz'></ul><sub id='lxgIz'></sub></form><legend id='lxgIz'></legend><bdo id='lxgIz'><pre id='lxgIz'><center id='lxgIz'></center></pre></bdo></b><th id='lxgIz'></th></span></q></dt></tr></i><div id='lxgIz'><tfoot id='lxgIz'></tfoot><dl id='lxgIz'><fieldset id='lxgIz'></fieldset></dl></div>
                  <tfoot id='lxgIz'></tfoot>
                3. <legend id='lxgIz'><style id='lxgIz'><dir id='lxgIz'><q id='lxgIz'></q></dir></style></legend>