UINavigationController popToRootViewController方法不能由委托调用吗? [英] UINavigationController popToRootViewController method cannot be called by a delegate?

查看:244
本文介绍了UINavigationController popToRootViewController方法不能由委托调用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很棒的UINavigationController。每个视图控制器都有其自己的按钮,该按钮将堆栈弹出回到其根,这也很好用。但是,我还希望能够通过按下选项卡栏上的按钮将堆栈弹出回到其根(显然,这是导航堆栈之外的一个完全不同的类)。

I have a UINavigationController which works great. Each view controller has its own button that pops the stack back to its root which also works great. However, I'd like to also be able to pop the stack back to its root by pressing a button on the tab bar (which is obviously in an entirely different class outside of the navigation stack).

因此,我在标签栏类中创建了一个委托,该委托在堆栈顶部找到了视图控制器,并在该视图控制器中调用该方法以将堆栈弹出回到根目录。我在控制台上打印了一些东西,以验证委托是否正确设置。一切都按其应有的方式进行,只是按下标签栏不会将堆栈弹出回到其根。

Therefore, I created a delegate in the tab bar class which finds the view controller at the top of the stack and calls the method in that view controller to pop the stack back to the root. I printed something to the console to verify that the delegate is set up correctly and it is. Everything works exactly as it should, except that pressing the tab bar doesn't pop the stack back to its root.

有想法吗?

这是UINavigationController堆栈顶部的视图控制器

class BlankViewController202: UIViewController, MainContainerViewControllerDelegate {

    // pop to root
    func popToRoot() {
        self.navigationController?.popToRootViewController(animated: true)
        print("success")
    }

}

在此功能之上从视图控制器内部被调用(当用户按下视图控制器本身上的按钮时),它将弹出堆栈。但是,当委托人从选项卡栏中调用相同的方法时,它不会弹出堆栈(但它确实会打印到控制台,因此我知道它已正确连接)。



这是按钮位于选项卡栏中的位置,当按下该按钮时,会将堆栈弹出回到其根目录

When this function above is called from within the view controller (when the user presses the button on the view controller itself), it pops the stack. But when this same exact method is called by a delegate from the tab bar, it doesn't pop the stack (but it does print to console so I know its hooked up properly).

This is where the button resides in the tab bar that when pressed should pop the stack back to its root

protocol MainContainerViewControllerDelegate {
    func popToRoot()
}

class MainContainerViewController: UIViewController {

    func moveToTab3(sender: UIButton!) {
        // ...
        let banana = BlankViewController202()
        self.delegate = banana
        delegate?.popToRoot()
    }

}


推荐答案

问题是 BlankViewController202()生成了一个全新的单独的BlankViewController202-不是 接口中已经存在的特定BlankViewController202作为导航控制器界面的一部分。您要与之交谈的是 BlankViewController202。

The problem is that BlankViewController202() makes a whole new, separate BlankViewController202 — it is not the particular BlankViewController202 that is already in the interface as part of the navigation controller interface. It is that BlankViewController202 you want to talk to.

这篇关于UINavigationController popToRootViewController方法不能由委托调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆