天天看点

iOS app开发架构

首页,我们来讲讲一般的软件都有神马组成:

1.window.rootViewController

2.UINavigationController

3.UITabBarController  其每一个标签也可能是导航控制器,,

4.用户引导界面

5.登陆界面

············

架构1:

由导航栏来控制引导页面,登陆界面,和UITabBarController的显示

QYHomeViewController *homeViewController = [[QYHomeViewController alloc] init]; //首页
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:homeViewController];
    nav.navigationBarHidden = YES;
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
    {
        QYUserGuideViewController *userGuide = [[QYUserGuideViewController alloc] init]; //如果是第一次登陆,出现引导页面
        [nav pushViewController:userGuide animated:YES];
        
    }else
    {
        QYLoginViewController *loginViewController = [[QYLoginViewController alloc] init];//否则,出现登陆页面
        [nav pushViewController:loginViewController animated:YES];
    }
    self.window.rootViewController = nav;
  
           

这种架构是由nav也就是导航控制器控制这几个主要界面的显示,就想当用户点击注销登录的时候,nav就会把登陆界面push出来。

缺点,程序一开始就会把UITabBarController,登陆界面,引导界面(第一次登陆)创建好,登陆之后,把引导界面和登陆界面pop走,(nav就想是一个栈,先进后出),但是UITabBarController一直都在内存中,

架构2 

不需要nav导航控制器来控制几个主要界面,我们创建一个继承与NSObject的管理类,该类是一个单例类,由他来来控制主要界面的呈现,谁要呈现管理类就把谁付给window.rootViewcontroller  ,需要谁创建谁

SGViewControllerManger.h

typedef NS_ENUM(NSUInteger, SGViewcontrollerType)
{
    SGControllerTypeUserGuide,
    SGControllerTypeLogin,
    sgControllerTypeMainView
    
};
@interface SGViewControllerManger : NSObject

+(void)presentViewControllerType:(SGViewcontrollerType)type;

@end
           

SGViewControllerManger.m

//把循环的部分抽离出来
+(void)presentViewControllerType:(SGViewcontrollerType)type
{
    
//    [[self alloc]init]是实例化一个SGViewControllerManger类,然后调用controllerType方法
        UIViewController *viewController = [[[self alloc]init] controllerType:type];
        UIWindow *mianWindow = [[[UIApplication sharedApplication] delegate] window];
    
        mianWindow.rootViewController = viewController;  
}
-(UIViewController *)controllerType:(SGViewcontrollerType)type
{

    UIViewController *viewController = nil;
    switch (type) {
        case SGControllerTypeLogin:
            viewController = [[SGLOginViewController alloc]init];
            break;
        case sgControllerTypeMainView:
            viewController = [[SGHomeViewController alloc]init];
            break;
        case SGControllerTypeUserGuide:
            viewController = [[SGUserGuideViewController alloc]init];
            break;
            
        default:
            break;
    }

    return viewController;
}
           

判断引导界面和登陆界面(是个否第一次登陆)

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        
        [SGViewControllerManger presentViewControllerType:SGControllerTypeUserGuide];
    }else{
        if (isRemenberPassWord) {
            [SGViewControllerManger presentViewControllerType:sgControllerTypeMainView];
        }else{
            
            [SGViewControllerManger presentViewControllerType:SGControllerTypeLogin];
        }
        
    }
           

点击下载架构1源码 架构Demo1    点击下载架构2源码   架构2Demo