在iOS开发中,主要的架构方式有两种, 这两种都需要考虑在项目开始时的几种情况:
1.第一次登录使用。2.不是第一次登录,且没有记住密码。3.不是第一次登录,但是记住了密码。
针对上面的三种情况两种架构有所不同,下面先说明第一种架构的实现
#import "QYAppDelegate.h" #import "QYUserGuideViewController.h" #import "QYHomeViewController.h" #import "QYLoginViewController.h" @implementation QYAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 使用[NSUserDefaults standardUserDefaults]这个单例对象来向磁盘中写入判断的结果 if (![[NSUserDefaults standardUserDefaults] boolForKey :@"everLaunched"]) { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"]; [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"]; } else{ [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"]; } //注意它的重要性,强制USerDefaults 将标志位写到磁盘去 [[NSUserDefaults standardUserDefaults] synchronize]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // 以homeViewController作为根控制器,来初始化导航控制器 QYHomeViewController *homeViewController = [[QYHomeViewController alloc]init]; self.nav = [[UINavigationController alloc] initWithRootViewController:homeViewController]; self.nav.navigationBarHidden = YES; NSLog(@"%@",self.nav); self.window.rootViewController = self.nav; // 对上述的三种情况的判断,进而怎样显示程序 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) { QYUserGuideViewController *userGuideController = [[QYUserGuideViewController alloc] init]; [self.nav pushViewController:userGuideController animated:YES]; } [self.window makeKeyAndVisible]; return YES; } @end
对于进入homeViewController后如何进行,页面的切换如下:
.h文件
#import <UIKit/UIKit.h> #import "QYChatTableViewController.h" #import "QYContactTableViewController.h" #import "QYAboutMeViewController.h" #import "QYFindViewController.h" @interface QYHomeViewController : UITabBarController @end
.m文件#import "QYHomeViewController.h" #import "QYUserGuideViewController.h" @interface QYHomeViewController () @end @implementation QYHomeViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // homeViewController是tabBarViewController的类别的扩展 // 分别以需要切换的页面的controller来初始化导航控制器 // 最后把导航控制器作为tabbarViewController的切换的数组 QYChatTableViewController *chatViewController = [[QYChatTableViewController alloc] initWithStyle:UITableViewStylePlain]; UINavigationController *navChat = [[UINavigationController alloc] initWithRootViewController:chatViewController]; QYContactTableViewController *contactViewController = [[QYContactTableViewController alloc] initWithStyle:UITableViewStylePlain]; UINavigationController *navContact = [[UINavigationController alloc] initWithRootViewController:contactViewController]; QYFindViewController *findViewController = [[QYFindViewController alloc] initWithStyle:UITableViewStylePlain]; UINavigationController *navFind = [[UINavigationController alloc] initWithRootViewController:findViewController]; QYAboutMeViewController *aboutViewController = [[QYAboutMeViewController alloc] initWithStyle:UITableViewStylePlain]; UINavigationController *navAboutMe = [[UINavigationController alloc] initWithRootViewController:aboutViewController]; self.viewControllers =@[navChat,navContact,navFind ,navAboutMe]; }
以上就基本实现了,工程项目的第一种架构,并能实现主界面的简单切换对于错漏之处,还请担待,也欢迎各位指出,对于第二种架构在下一篇博客中会进行简单介绍。
iOS开发中常用的两种架构(1)
最新推荐文章于 2022-06-27 00:00:00 发布