自定义控件里如何拿到导航控制器进行页面跳转?
(1)如果有UITabBarController我们会这样获取导航控制器:
UIViewController *viewC = [[UIViewController alloc]init];
// 取出当前的导航控制器
UITabBarController *tabBarVc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//The view controller associated with the currently selected tab item
//当前选择的导航控制器
UINavigationController *navC = (UINavigationController *)tabBarVc.selectedViewController;
[navC pushViewController:viewC animated:YES];
(2)如果通过modal出来的控制器并且用UITabBarController不好使, 我们会这样获取导航控制器:
UIViewController *viewC = [[UIViewController alloc]init];
//获取最终的根控制器
UIViewController *rootC = [UIApplication sharedApplication].keyWindow.rootViewController;
//如果是modal出来的控制器,它就会通过presentedViewController拿到上一个控制器
UINavigationController *navC = (UINavigationController *)rootC.presentedViewController;
[navC pushViewController:viewC animated:YES];