UrbanApps/Armchair

getRootViewController won't work correctly.

RyogaK opened this issue · 2 comments

I'm not in detail, but nextResponder of the item in keyWindow.subviews is keyWindow in my case.
I think it should be discussed.

How about use following logic?
At least, the following works for me.

var queuedSubviews = [UIView]()
queuedSubviews.appendContentsOf(window.subviews)

while !queuedSubviews.isEmpty {
    let subView = queuedSubviews.removeFirst()
    if let responder = subView.nextResponder() {
        if responder.isKindOfClass(UIViewController) {
            return topMostViewController(responder as? UIViewController)
        }
    }
    queuedSubviews.appendContentsOf(subView.subviews)
}

I can make pull request if needed 😆

I'm not sure iterating through the view controllers is the right approach. There are a lot of assumptions that need to be made, and even then the view controller you find might not currently be in the right state to present a modal because it is in the process of transitioning.

Perhaps a better approach would be to create a new window with an empty view controller as root and present from that view controller. The dismissal of the modal can then hide the widow and release it. This is the approach that I have used in situations like this and it works well.

@scottrhoyt That makes sense. your approach sounds good to me. Thank you!