ios - 尝试在其 View 不在窗口层次结构中的 firstController 上呈现 <UINavigationController>

标签 ios iphone swift

我尝试在一个函数中打开第二个 View Controller ,但出现此错误:

Attempt to present <UINavigationController> on firstController whose view is not in the window hierarchy!

所需的所有解决方案:

要么把代码放在viewWillAppear()里面 或里面 IBAction函数

我的程序需要从普通函数打开第二个 View Controller

我试过所有这些代码:

代码1:

self.performSegueWithIdentifier("page2", sender:self)

结果:

error: has no segue with identifier 'page2''

代码 2:

let secondViewController:page2 = page2()
self.presentViewController(secondViewController, animated: true,completion: nil)

结果:

error: whose view is not in the window hierarchy!

代码 3:

let nextViewController = storyboard!.instantiateViewControllerWithIdentifier("next2") as! UIViewController
self.presentViewController(nextViewController, animated:true, completion:nil)

结果:

fatal error: unexpectedly found nil fwom win func

代码 4:

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("next2") as! page2
self.presentViewController(nextViewController, animated:true, completion:nil)
  dismissViewControllerAnimated(true, completion:nil)

结果:

error whose view is not in the window hierarchy!

我需要一个解决方案。

最佳答案

您将要使用代码 1,但是在您的 Storyboard 中,您将需要从包含您的代码的 ViewController 到您要显示的 ViewController 的转场。 (Ctrl+从 ViewController“所有者”图标拖动到第二个 ViewController)

然后单击此转场,并在检查器中确保转场 (!) 获得名称“page2”——因为此名称用于运行转场。

关于ios - 尝试在其 View 不在窗口层次结构中的 firstController 上呈现 <UINavigationController>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34762267/

相关文章:

ios - 为 NSNumber 值设置精度

iphone - 用户选择允许后如何处理启用位置服务?

iphone - 如何使用 CoreData 进行随机排序

IOS 'NSInvalidArgumentException',原因: 'data parameter is nil'

ios - Xcode 开发 : save api response for future tests during development

ios - 如何关闭 MFMailComposeViewController?

ios - 减慢 UIScrollView 滚动

swift - 泛型类型 'DataResponse' 专用于类型参数太少(得到 1,但预期为 2)

iphone - 将方法与按钮 "touch up inside"操作集成

ios - iPhone XS 设备上的 iOS 15 Beta 'Untrusted Developer' 上的 Xcode 13 错误