简体   繁体   English

在其他选项卡中弹出视图控制器

[英]popping a view controller in different tab

UPDATE This code appears to do what I want, but I am curious if this is a bad tactic now. 更新此代码似乎可以执行我想要的操作,但是我很想知道这是否是个坏策略。

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    print("pop view controller from tab bar")

    if tabBarController.selectedIndex == 2 {
        print("appdel detected index 2 tab")
        let navCont = viewController as! UINavigationController
        navCont.popToRootViewControllerAnimated(true)
    }
}

ORIGINAL QUESTION: I have some code that will delete data another tab refers to. 原始问题:我有一些代码可以删除另一个标签所指的数据。 I want to make sure that when this code executes in [Edit Record VC] that it will force tab to pop back to the root view, even though the code executes on its own tab: 我想确保在[Edit Record VC]中执行此代码时,即使代码在其自己的选项卡上执行,也将迫使选项卡弹出回到根视图:

[ ---------------TAB BAR CONTROLLER ------------------ ]
  TAB 0              TAB 1         TAB 2           TAB 3
     |                 |              |              |
     |                 |              |              |
  [NAV CONTR 0]  [NAV CONTR 1]  [NAV CONTR 2]  [NAV CONTR 3]
                       |              |        
                       |              |  
                   [Table VC]      [Map VC]
                       \              /
                        \            /
                         \          /
                       [View Record VC]
                               |
                               |
                       [Edit Record VC]

In the above drawing, when I delete a record in [Edit Record VC], I want to pop Nav Controller 1 and Nav Controller 2 to the very first VC. 在上图中,当我删除[Edit Record VC]中的记录时,我想将Nav Controller 1和Nav Controller 2弹出到第一个VC。 How can I do this? 我怎样才能做到这一点? I have tried every way I can think of and the code just isn't working. 我已经尝试了所有可以想到的方法,但是代码却无法正常工作。

The reason I want to do this is because if NAV CONTR 2 navigates to [Edit Record VC] and deletes the record, [View Record VC] will still be referencing that record, causing it to point to a deleted object. 我要执行此操作的原因是,如果NAV CONTR 2导航到[编辑记录VC]并删除了该记录,则[查看记录VC]仍将引用该记录,导致其指向已删除的对象。 When I try to open the NAV CONTR 2 tab after deletion, it'll cause a crash 删除后尝试打开NAV CONTR 2标签时,会导致崩溃

Multiple objects reacting to an event sounds like a job for NSNotification . NSNotification事件的多个对象听起来像是NSNotification的工作。

Make NAV CONTR 1 and NAV CONTR 2 instances of a navigation controller subclass that listens for a notification ( resetToFirstController or something). 使导航控制器子类的NAV CONTR 1和NAV CONTR 2实例侦听通知( resetToFirstController类)。 Make posting that notification part of the delete logic. 使发布该通知成为删除逻辑的一部分。

When each controller receives the notification it pops to its root...or whichever "safe" controller you choose. 当每个控制器接收到通知时,它会弹出其根目录...或您选择的任何“安全”控制器。

I wanted to show Phillip Mills actual implementation that I did that worked like a charm. 我想向菲利普·米尔斯(Phillip Mills)展示我的实际实现方式,使它发挥了作用。

First please refer to this post on NSNotification, ( NotificationCenter issue on Swift 3 ), this is a very good reference on the syntax in swift3. 首先,请参阅有关NSNotification的文章( Swift 3上的NotificationCenter问题 ),这是swift3中语法的很好参考。

So in the controller that you want to have the ability to pop to the root controller of the navigation you will need to add the following function with print being optional, but useful so you see what is going on. 因此,在您希望能够弹出导航的根控制器的控制器中,您将需要添加以下功能,其中print是可选的,但很有用,因此您可以看到发生了什么。

    // MARK: - Notifications
    func resetToTopView(notification: NSNotification){
        _ = navigationController?.popToViewController(self, animated: true)
        print("poppedViewController for TABNAMEHERE Successfully!")
    }

Next you need to register this function so it can be called by the Notification center. 接下来,您需要注册此功能,以便通知中心可以调用它。 Simply in the viewDidLoad of the same controller add the following NotificationCenter code. 只需在同一控制器的viewDidLoad中添加以下NotificationCenter代码。

func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(self.resetToTopView(notification:)), name: k_resetToTopViewForYourTabNotification, object: nil)
}

The code above will give an error if you do not set the constant in your program for k_resetToTopViewForYourTabNotification. 如果您未在程序中为k_resetToTopViewForYourTabNotification设置常量,则以上代码将给出错误。 I usually create a file somewhere called globals where I put these constants. 我通常在一个名为globals的地方创建一个文件,在其中放置这些常量。

To define it put something like this there. 为了定义它,在这里放这样的东西。

let k_resetToTopViewForYourTabNotification = Notification.Name("resetToTopViewForYourTabNotification")

Then in the other tab or for that matter anywhere else in your code that you are, even a different tab, if you want to call the function to pop the view controllers back to the root, you simply call this command. 然后,在另一个选项卡中或就此而言,您在代码中的其他任何位置(甚至是其他选项卡),如果要调用该函数以将视图控制器弹出到根,只需调用此命令即可。

NotificationCenter.default.post(name: k_resetToTopViewForYourTabNotification, object: nil)

Really simple and nice :). 真的很简单,也很不错:)。

First Case :When you want to select other tab index 第一种情况 :要选择其他选项卡索引时

guard let VCS = self.navigationController?.viewControllers else {return }
for controller in VCS {
    if controller.isKind(of: TabBarController.self) {
        let tabVC = controller as! TabBarController
        tabVC.selectedIndex = index . (Select any index for tab)
        self.navigationController?.popToRootViewController(animated: true)
    }
}

Second Case: When you want to access to RootViewController variables 第二种情况:当您想访问RootViewController变量时

guard let VCS = self.navigationController?.viewControllers else {return }
for controller in VCS {
    if controller.isKind(of: TabBarController.self) {
        let tabVC = controller as! TabBarController
        //    tabVC.selectedIndex = 0 . //no need of this line if you want to access same tab where you have started your navigation
        let VCs = tabVC.selectedViewController as! MyViewController
        VCs.variableName = true . //access your variable
        self.navigationController?.popToRootViewController(animated: true)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM