getRootViewController won't work correctly. #49

Closed
opened 2025-12-29 18:28:41 +01:00 by adam · 2 comments
Owner

Originally created by @RyogaK on GitHub (Oct 20, 2016).

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 😆

Originally created by @RyogaK on GitHub (Oct 20, 2016). 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 😆
adam closed this issue 2025-12-29 18:28:42 +01:00
Author
Owner

@scottrhoyt commented on GitHub (Feb 13, 2017):

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 commented on GitHub (Feb 13, 2017): 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.
Author
Owner

@RyogaK commented on GitHub (Feb 14, 2017):

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

@RyogaK commented on GitHub (Feb 14, 2017): @scottrhoyt That makes sense. your approach sounds good to me. Thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Armchair#49