0
我試圖從VC1展示VC2而不使用segue。有用。然後,我試圖使用self.navigationController?.popViewControllerAnimated(true)
回來,但它不起作用。我想知道我應該用什麼代碼從VC2退回到VC1。下面的代碼在appDelegate中。當前視圖控制器不是由segue呈現時返回。 Swift
的AppDelegate
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let VC2 = storyboard.instantiateViewControllerWithIdentifier("VC2") as! VC2
let navController = UINavigationController(rootViewController: VC2)
self.topViewController()!.presentViewController(navController, animated: false, completion: nil)
func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
if let MMDrawers = base as? MMDrawerController {
for MMDrawer in MMDrawers.childViewControllers {
return topViewController(MMDrawer)
}
}
if let nav = base as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(presented)
}
return base
}
VC2
@IBAction func backButtonTapped(sender: AnyObject) {
print(self.navigationController?.viewControllers) // print([<MyAppName.VC2: 0x12f147200>])
self.navigationController?.popViewControllerAnimated(true)
}
打印self.navigationcontroller.viewcontrollers並在backbuttonTapped方法中放置一個斷點並相應地更新代碼 –
我試圖打印(self.navigationController?.viewControllers)。它會打印[] –
Tony
準確地說,它不會回到上一個屏幕,因爲您正在呈現一個視屏並嘗試使用popviewcontroller無法工作,請使用dismissviewcontroller它將返回到上一個屏幕。 –