Attempt to present ViewController whose view is not in the windows hierarchy

前端 未结 6 1487
一整个雨季
一整个雨季 2020-12-14 13:34

I meet a strange problem: I made 2 view controllers for wich I can switch the view with code:

var currentViewController:UIViewController=UIApplication.shared         


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 14:08

    Use the extension below to retrieve the next available controller in the stack.

    Swift 3

    extension UIResponder {
        func next(_ type: T.Type) -> T? {
            return next as? T ?? next?.next(type)
        }
    }
    

    Swift 2.3

    extension UIResponder {
        func nextResponder(_ type: T.Type) -> T? {
            return nextResponder() as? T ?? nextResponder()?.nextResponder(type)
        }
    }
    

    Inside your SKScene, view?.next(UIViewController.self) gives you the next available UIViewController in the hierarchy.

    Add this extension to a group in your project called Categories, if this group does not exist already create it, then create a new file called UIResponder+NextOfType.swift and paste the extension.

提交回复
热议问题