简体   繁体   English

问题过渡到根视图控制器

[英]Problem transitioning to root view controller

I have a storyboard setup like this: 我有一个故事板设置,如下所示:

Home Nav Controller - VC1 - VC2 - VC3 - VC4 家用导航控制器-VC1-VC2-VC3-VC4

Settings Nav Controller - VC1 - VC2 - VC3 设置导航控制器-VC1-VC2-VC3

To go between Home screens and Settings screens, I have a home icon/button in Settings VC1; 要在主屏幕和设置屏幕之间切换,我在设置VC1中有一个主图标/按钮; and a settings icon/button in Home VC1. 以及Home VC1中的设置图标/按钮。 But I am not able to get this transition work correctly. 但是我无法正确完成此过渡工作。

First I tried this code. 首先,我尝试了这段代码。

@IBAction func homeButtonTapped(button: UIButton) {
    var navController: AppStoryboard.Main.instance.instantiateViewController(withIdentifier: "HomeNavController") as? UINavigationController

    currentViewController?.present(navController!, animated: true, completion: nil)
}

//... similar code for settingsButtonTapped

It works fine. 工作正常。 But this creates multiple view controller instances (checked Debug Memory Graph). 但这会创建多个视图控制器实例(已选中“调试内存图”)。 Say I go back and forth between Home VC1 and Settings VC1 5 times, I see 5 instances of each view controller. 假设我在Home VC1和Settings VC1之间来回5次,我看到每个视图控制器有5个实例。

Then I tried this. 然后我尝试了这个。

@IBAction func homeButtonTapped(button: UIButton) {
    var navController: AppStoryboard.Main.instance.instantiateViewController(withIdentifier: "HomeNavController") as? UINavigationController

    window?.rootViewController = navController!
}

//... similar code for settingsButtonTapped

This fixed the problem of multiple instances. 这解决了多个实例的问题。 But it messes up the custom animation popups I have. 但这弄乱了我的自定义动画弹出窗口。 The popups work fine if I never ever click the Home or Settings button. 如果我从不单击“主页”或“设置”按钮,则弹出窗口可以正常工作。 But if I click the button once, from that points onwards the popups are not displayed correctly. 但是,如果我单击一次按钮,从那以后,弹出窗口将无法正确显示。 Say I go from VC1 to VC2, and then open a popup in VC2. 假设我从VC1转到VC2,然后在VC2中打开一个弹出窗口。 The popup appears, but the background is not greyed out like it usually is, and I can see the contents of VC2 clearly, and behind that I can actually see contents of VC1 too. 弹出窗口出现了,但是背景并没有像通常那样变灰,并且我可以清楚地看到VC2的内容,而在后面我也可以实际看到VC1的内容。

Any suggestion on how to get this working? 关于如何使它工作的任何建议?

Thanks. 谢谢。

You should declare wich of the two is visible first so if we assume that Home is the first one showed to users, pressing on settings you should present it like this 您应该声明这两者中的第一个是可见的,因此,如果我们假设首页是向用户显示的第一个,按设置,您应该像这样显示它

@IBAction func settingsButtonTapped(button: UIButton) {
    let navController = // Instantiate your settings controller
    present(navController!, animated: true, completion: nil)
}

Then if you want to go back to the home view controller you should dismiss the settings view controller like this: 然后,如果您想回到主视图控制器,则应关闭设置视图控制器,如下所示:

@IBAction func homeButtonTapped(button: UIButton) {
    presentingViewController?.dismiss(animated: true, completion: nil)
}

Every time you present a view controller this is displayed above the view controller currently displayed and it covers it but the first view controller is not removed and deallocated. 每次您提供一个视图控制器时,它都会显示在当前显示的视图控制器上方,并且会覆盖它,但是不会删除和释放第一个视图控制器。

You should only present one Navigation Controller and i think it should be the SettingViewController 您应该只展示一个导航控制器,我认为应该是SettingViewController

@IBAction func SettingButtonTapped(button: UIButton) {
    var navController: AppStoryboard.Main.instance.instantiateViewController(withIdentifier: "SettingNavController") as? UINavigationController

    currentViewController?.present(navController!, animated: true, completion: nil)
}

But in homeButtonTapped you should just dismiss the controller 但是在homeButtonTapped您应该关闭控制器

@IBAction func homeButtonTapped(button: UIButton) {
   self.navigationController?.dismiss(animated: true, completion: nil)
}

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

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