Using presentViewController from UIView

后端 未结 5 479
鱼传尺愫
鱼传尺愫 2020-12-13 14:52

I am creating a title bar for my iOS application and I am making this inside a UIView. The only issue I am having is with the \"home button\". When the home button is presse

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 15:40

    You can do this without using the delegate pattern. Here you go

    ObjectiveC

    UIViewController *currentTopVC = [self currentTopViewController];
    currentTopVC.presentViewController......... 
    
    - (UIViewController *)currentTopViewController {
        UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
        while (topVC.presentedViewController) {
            topVC = topVC.presentedViewController;
        }
        return topVC;
    }
    

    Swift

    var topVC = UIApplication.sharedApplication().keyWindow?.rootViewController
    while((topVC!.presentedViewController) != nil) {
         topVC = topVC!.presentedViewController
    }
    topVC?.presentViewController........
    

提交回复
热议问题