获取当前控制器

整数字面值可以写为:
一个十进制数,没有前缀
一个二进制数,前缀是 0b
一个八进制数,前缀是 0o
一个十六进制数,前缀是 0x
设置textfield的placeholderLabel的颜色
 [self.textField setValue:LineColor forKeyPath:@"placeholderLabel.textColor"];
 self.textField.placeholder = @"这的字不影响placeholder,但是不能为空";
获取当前的控制器
- (UIViewController *)getCurrentVC{
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal){
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows){
            if (tmpWin.windowLevel == UIWindowLevelNormal){
                window = tmpWin;
                break;
            }
        }
    }
    id  nextResponder = nil;
    UIViewController *appRootVC = window.rootViewController;
    if (appRootVC.presentedViewController) {
        nextResponder = appRootVC.presentedViewController;
    }else{
        UIView *frontView = [[window subviews] objectAtIndex:0];
        nextResponder = [frontView nextResponder];
//        nextResponder = appRootVC;//与上面有何区别
    }
    
    if ([nextResponder isKindOfClass:[UITabBarController class]]){
        UITabBarController * tabbar = (UITabBarController *)nextResponder;
        UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
//        UINavigationController * nav = tabbar.selectedViewController;
       return nav.childViewControllers.lastObject;
    }else if ([nextResponder isKindOfClass:[UINavigationController class]]){
        UIViewController * nav = (UIViewController *)nextResponder;
        return nav.childViewControllers.lastObject;
    }else{
        return nextResponder;
    }
}

你可能感兴趣的:(获取当前控制器)