Get to UIViewController from UIView?

前端 未结 29 1736
一整个雨季
一整个雨季 2020-11-22 16:57

Is there a built-in way to get from a UIView to its UIViewController? I know you can get from UIViewController to its UIView

29条回答
  •  清酒与你
    2020-11-22 17:36

    If your rootViewController is UINavigationViewController, which was set up in AppDelegate class, then

        + (UIViewController *) getNearestViewController:(Class) c {
    NSArray *arrVc = [[[[UIApplication sharedApplication] keyWindow] rootViewController] childViewControllers];
    
    for (UIViewController *v in arrVc)
    {
        if ([v isKindOfClass:c])
        {
            return v;
        }
    }
    
    return nil;}
    

    Where c required view controllers class.

    USAGE:

         RequiredViewController* rvc = [Utilities getNearestViewController:[RequiredViewController class]];
    

提交回复
热议问题