ios swift无法关闭视图控制器
我正在像这样打开新的viewcontrollerios swift无法关闭视图控制器,ios,swift,Ios,Swift,我正在像这样打开新的viewcontroller let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let view_controller1 = storyBoard.instantiateViewController(withIdentifier: "incident_view_id") as! IncidentVi
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let view_controller1 = storyBoard.instantiateViewController(withIdentifier: "incident_view_id") as! IncidentViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
//self.present(registerViewController, animated: true)
let navigationController = UINavigationController.init(rootViewController: view_controller1)
appDelegate.window?.rootViewController = navigationController
这很好
现在,当我按下一个按钮,我需要关闭当前视图并返回上一个视图,我累了
self.navigationController?.popToRootViewController(animated: true)
但是不行,我也试过了
self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)
但结果是一样的。您以错误的方式显示了
ViewController
。下一行告诉应用程序,无论您打开了什么ViewControllers
,只要将其抛出并将您正在创建的UINavigationController
指定为rootViewController即可。因此,您的代码(解除
)不起作用的原因是,在当前的视图控制器之后没有其他的视图控制器
appDelegate.window?.rootViewController = navigationController // Wrong
这通常会进入应用程序的开头,在这里定义要启动应用程序的UIViewController
。我相信您正在调用从UIViewController
发布的第一个代码块。因此,您需要做的不是创建新的UINavigationController
并将其分配给rootViewController
,而是使用UIViewController
s导航控制器推送下一个UIViewController
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let view_controller1 = storyBoard.instantiateViewController(withIdentifier: "incident_view_id") as! IncidentViewController
navigationController?.pushViewController(view_controller1, animated: true)
如果我理解您的代码,您将显示一个带有一个视图的导航控制器,并尝试从此视图弹出,但您的导航堆栈中只有一个视图。我有主视图控制器,即初始视图控制器,从那里我将使用故事板id如上所述打开一个新的视图控制器。感谢您的回答,我会检查它并报告。是的,这就是问题所在,现在代码可以与self.navigationController?.popToRootViewController(动画:true)
一起正常工作,很高兴它有所帮助。