美文网首页
切换 RootViewControll

切换 RootViewControll

作者: ldhonline | 来源:发表于2018-10-26 07:40 被阅读0次

    经常会在主 VC 前某些时候先显示其它的过渡页面,这个时候就要切换根视图

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        
        BOOL isFirstStart = YES;
        self.window.backgroundColor = [UIColor whiteColor];
        if(isFirstStart){
            self.window.rootViewController = [AlertViewController new];
        }else{
            [self swtchToDefaultRootViewController];
        }
        
        [self.window makeKeyAndVisible];
        
        return YES;
    }
    
    -(void)swtchToDefaultRootViewController{
        [self.window.rootViewController.view removeFromSuperview];
        self.window.rootViewController = [ViewController new];
    }
    
    

    第一个 VC

    //
    //  AlertViewController.m
    //  mjtable
    //
    //  Created by ldhonline on 2018/10/26.
    //  Copyright © 2018年 aidoutu.com. All rights reserved.
    //
    
    #import "AlertViewController.h"
    #import "masonry.h"
    #import "AppDelegate.h"
    
    @interface AlertViewController ()
    
    @end
    
    @implementation AlertViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor lightGrayColor];
        
        UIButton *btn = [[UIButton alloc] init];
        
        [self.view addSubview:btn];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self.view);
            make.width.mas_equalTo(200);
            make.height.mas_equalTo(40);
        }];
        
        btn.backgroundColor = [UIColor grayColor];
        [btn setTitle:@"关闭" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(dimss) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    - (void)dimss{
        AppDelegate *currentAppDelegate = (AppDelegate *)([UIApplication sharedApplication].delegate);
        [currentAppDelegate swtchToDefaultRootViewController];
    }
    
    
    @end
    
    
    

    相关文章

      网友评论

          本文标题:切换 RootViewControll

          本文链接:https://www.haomeiwen.com/subject/mkohtqtx.html