How to find topmost view controller on iOS

后端 未结 30 2318
遥遥无期
遥遥无期 2020-11-22 08:40

I\'ve run into a couple of cases now where it would be convenient to be able to find the \"topmost\" view controller (the one responsible for the current view), but haven\'t

30条回答
  •  粉色の甜心
    2020-11-22 09:07

    Not sure if this will help what you're trying to accomplish by finding the topmost view controller, but I was trying to present a new view controller, but if my root view controller already had a modal dialog, it would be blocked, so I would cycle to the top of all modal view controllers using this code:

    UIViewController* parentController =[UIApplication sharedApplication].keyWindow.rootViewController;
    
    while( parentController.presentedViewController &&
           parentController != parentController.presentedViewController )
    {
        parentController = parentController.presentedViewController;
    }
    

提交回复
热议问题