警告:尝试在*窗口层次上显示*的*,而不是在窗口层次结构中

我试图提出一个ViewController如果在数据模型中有任何保存的数据。 但是,我得到了上面的错误:“警告:试图展示*上*其视图不在窗口层次”当下面的IF语句为真。

 override func viewDidLoad() { super.viewDidLoad() loginButton.backgroundColor = UIColor.orangeColor() var request = NSFetchRequest(entityName: "UserData") request.returnsObjectsAsFaults = false var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate) var context:NSManagedObjectContext = appDel.managedObjectContext! var results:NSArray = context.executeFetchRequest(request, error: nil)! if(results.count <= 0){ print("Inga resultat") } else { print("SWITCH VIEW PLOX") let internVC = self.storyboard?.instantiateViewControllerWithIdentifier("internVC") as internViewController self.presentViewController(internVC, animated: true, completion: nil) } } 

Iv'e尝试了使用Googlefind的不同解决scheme。

干杯。

在你的代码中,这个视图控制器的视图只是被创build的,而没有被添加到任何视图层次结构中。 如果你想尽快从视图控制器呈现你应该做的事情,看看是否是最安全的。

在目标c:这解决了我的问题时,在mpmovieplayer上呈现viewcontroller

 - (UIViewController*) topMostController { UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; while (topController.presentedViewController) { topController = topController.presentedViewController; } return topController; } 

Swift 3

我有这个作为一个新手不断发现,发现目前加载模态的意见,可以被驳回,但切换到根控制器是最好的,如果你不需要显示模式。

我正在使用这个

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard?.instantiateViewController(withIdentifier: "MainAppStoryboard") as! TabbarController present(vc, animated: false, completion: nil) 

用这个代替我的tabController:

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let view = storyboard.instantiateViewController(withIdentifier: "MainAppStoryboard") as UIViewController let appDelegate = UIApplication.shared.delegate as! AppDelegate //show window appDelegate.window?.rootViewController = view 

如果您需要在多个故事板屏幕之间切换,只需调整视图控制器即可。

为SWIFT

 func topMostController() -> UIViewController { var topController: UIViewController = UIApplication.sharedApplication().keyWindow!.rootViewController! while (topController.presentedViewController != nil) { topController = topController.presentedViewController! } return topController } 

你只需要延迟执行一个select器 – (0秒的作品)。

  override func viewDidLoad() { super.viewDidLoad() perform(#selector(presentExampleController), with: nil, afterDelay: 0) } func presentExampleController() { let exampleStoryboard = UIStoryboard(named: "example", bundle: nil) let exampleVC = storyboard.instantiateViewController(withIdentifier: "ExampleVC") as! ExampleVC present(exampleVC, animated: true) } 

Swift 3。

调用这个函数来获得最顶层的视图控制器,然后让视图控制器存在。

 func topMostController() -> UIViewController { var topController: UIViewController = UIApplication.shared.keyWindow!.rootViewController! while (topController.presentedViewController != nil) { topController = topController.presentedViewController! } return topController } 

用法:

 let topVC = topMostController() let vcToPresent = self.storyboard!.instantiateViewController(withIdentifier: "YourVCStoryboardID") as! YourViewController topVC.present(vcToPresent, animated: true, completion: nil) 

对于迅速3.0及以上

 public static func getTopViewController() -> UIViewController?{ if var topController = UIApplication.shared.keyWindow?.rootViewController { while (topController.presentedViewController != nil) { topController = topController.presentedViewController! } return topController } return nil} 
 let storyboard = UIStoryboard(name: "test", bundle: nil) let vc = storyboard.instantiateViewController(withIdentifier: "teststoryboard") as UIViewController UIApplication.shared.keyWindow?.rootViewController?.present(vc, animated: true, completion: nil) 

这似乎是确保这是最重要的观点。

我得到一个错误

警告:尝试在myapp.testController上显示myapp.testController:0x7fdd01703990:0x7fdd01703690其视图不在窗口层次结构中!

希望这可以帮助他人迅速3

我已经尝试了这么多的赞赏! 唯一有用的是:

 if var topController = UIApplication.shared.keyWindow?.rootViewController { while (topController.presentedViewController != nil) { topController = topController.presentedViewController! } } 

斯威夫特4

 func topMostController() -> UIViewController { var topController: UIViewController = UIApplication.shared.keyWindow!.rootViewController! while (topController.presentedViewController != nil) { topController = topController.presentedViewController! } return topController } 
Interesting Posts