gpt4 book ai didi

ios - UIApplication.sharedApplication().keyWindow?.rootViewController 卡住了我的应用程序

转载 作者:行者123 更新时间:2023-11-29 12:06:20 29 4
gpt4 key购买 nike

在我的应用程序中的某个时刻,我需要返回到我的主(即主页)ViewController。所以我需要向后移动到我的导航堆栈,它现在充满了 ViewControllers 和 NavigationControllers。所以我做了以下事情:

// to dismiss the current VC
self.navigationController?.popViewControllerAnimated(true)

// to go back home
UIApplication.sharedApplication().keyWindow?.rootViewController = homeNC

homeNC 是指向 NavigationController 的全局变量,其中包含我的主要“主页”ViewController 的 Storyboard实例。

它有效,但部分有效。意思是,它确实带我回到了我的主 ViewController,但它随后变得对触摸和滚动没有响应。没有任何响应,无论是导航栏按钮还是家庭 NC/VC 中的 TableViewCells。 Xcode 不会崩溃,只是坐在那里等待。

我不知道为什么。谁能想到我在这里缺少什么?

** 编辑 **

可能是因为我在我的主(家庭)VC 中嵌入了一个嵌套的 VC。这是代码以及我之后尝试的其他内容:

homeNC = self.navigationController!
homeVC = self

let discoverVC: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("DiscoverVC") as UIViewController

// embed discoverVC in this VC
self.addChildViewController(discoverVC)
self.view.addSubview(discoverVC.view)
discoverVC.didMoveToParentViewController(self)

// store self NC for using it by "thank you" VC to come back to home after payment
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

// navigates back but becomes unresponsive
appDelegate.window?.rootViewController = homeVC

// navigates back but becomes unresponsive
appDelegate.window?.rootViewController = homeNC

我还尝试使用以下内容保存对 appdelegate 窗口的引用:

// Initialise sideMenu
window = UIWindow(frame: UIScreen.mainScreen().bounds)

let sideMenu = SSASideMenu(contentViewController: UINavigationController(rootViewController: MainViewController()), leftMenuViewController: LeftMenuViewController())

window!.rootViewController = sideMenu
window!.makeKeyAndVisible()

// get a reference to appdelegate's window so that we can navigate back later
mainWindow = window!

但是当我这样做时它又不会导航回来:

appDelegate.window?.rootViewController = mainWindow.rootViewController

最佳答案

试试这个。我会为你工作。

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = mainVC

编辑

我读出了您编辑过的问题。 SSASideMenu 出现问题。我更新了我的答案,我希望它现在对你有用。您必须进行两项更改。首先在 AppDelegate.swift 中,像这样设置 SSASideMenu 的属性:

var window: UIWindow?
var sideMenu: SSASideMenu!

现在您的代码将更改为:

sideMenu = SSASideMenu(contentViewController: UINavigationController(rootViewController: MainViewController()), leftMenuViewController: LeftMenuViewController())

现在如果你想改变 View Controller ,你必须像这样改变 SSASideMenucontentViewController:

func showDiscoverVC() {
//Instantiate ViewController
let discoverVC: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("DiscoverVC") as UIViewController

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.sideMenu.contentViewController = UINavigationController(rootViewController: discoverVC)
}

关于ios - UIApplication.sharedApplication().keyWindow?.rootViewController 卡住了我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34860996/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com