Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ios swift无法关闭视图控制器_Ios_Swift - Fatal编程技术网

ios swift无法关闭视图控制器

ios 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

我正在像这样打开新的viewcontroller

            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)
一起正常工作,很高兴它有所帮助。