iOS之视图切换
//基类:通过该类创建自定义导航控制器 #import "CXDNavigationController.h" #import "UIColor+Extension.h"//设置颜色的工具类 @interface CXDNavigationController () @end @implementation CXDNavigationController - (void)viewDidLoad { [super viewDidLoad]; //动态更改导航背景/样式 UINavigationBar *bar = [UINavigationBar appearance]; [bar setBarTintColor:[UIColor colorWithHexString:@"#303030"]]; [bar setTintColor:[UIColor whiteColor]]; [bar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont boldSystemFontOfSize:20], }]; //导航条中按钮样式 UIBarButtonItem *item = [UIBarButtonItem appearance]; [item setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont boldSystemFontOfSize:20], } forState:UIControlStateNormal]; } //更改状态栏颜色(有信号,时间,电池等一栏) - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } //确定状态栏是否隐藏 -(BOOL)prefersStatusBarHidden { return NO; }
//调度:通过该类切换到对应视图控制器 //亮点:通过单例模式创建视图,减小内存消耗 //用户通过在侧滑菜单点击对应视图进行视图切换 #import "AllControllerDispatchTool.h" #import "GoodProductsViewController.h" #import "ReadingViewController.h" #import "SettingViewController.h" #import "CXDNavigationController.h" @implementation AllControllerDispatchTool #pragma mark - 调度方法 + (void)createViewControllerWithIndex:(NSUInteger)index { //获取当前类(唯一)对象 AllControllerDispatchTool *dispatchTool = [AllControllerDispatchTool shareOpenController]; //用当前类对象 执行实际选择执行方法 [dispatchTool openViewControllerWithIndex:index]; } + (instancetype)shareOpenController { //获取到调度的唯一对象 static AllControllerDispatchTool *tempTool = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ tempTool = [[AllControllerDispatchTool alloc] init]; }); return tempTool; } #pragma mark - 实际选择执行方法 - (void)openViewControllerWithIndex:(NSUInteger)index { switch (index) { case 0:{ //第一次加载,加载[良品]界面 static CXDNavigationController *navVC = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ GoodProductsViewController *goodVC = [[GoodProductsViewController alloc] init]; navVC = [[CXDNavigationController alloc] initWithRootViewController:goodVC]; }); [UIApplication sharedApplication].keyWindow.rootViewController = navVC; [[UIApplication sharedApplication].keyWindow makeKeyAndVisible]; } break; case 1:{ //第二次加载,加载[阅读]界面 static CXDNavigationController *navVC = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ ReadingViewController *readVC = [[ReadingViewController alloc] init]; navVC = [[CXDNavigationController alloc] initWithRootViewController:readVC]; }); [UIApplication sharedApplication].keyWindow.rootViewController = navVC; [[UIApplication sharedApplication].keyWindow makeKeyAndVisible]; } break; case 2:{ //第三次加载,加载[设置]界面 static CXDNavigationController *navVC = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ SettingViewController *settVC = [[SettingViewController alloc] init]; navVC = [[CXDNavigationController alloc] initWithRootViewController:settVC]; }); [UIApplication sharedApplication].keyWindow.rootViewController = navVC; [[UIApplication sharedApplication].keyWindow makeKeyAndVisible]; } break; default: break; } }
PS:部分代码,仅供参考,欢迎讨论
浙公网安备 33010602011771号