欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS-新手引导页+标签控制器

程序员文章站 2024-03-22 23:23:10
...

AppDelegate.h

#import "ViewController.h"
#import "MenuViewController.h"


@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong) NSPersistentContainer *persistentContainer;
@property (nonatomic,strong)MenuViewController *vc;
@property (nonatomic,strong)UITabBarController *tabBarCtl;//标签栏控制器

AppDelegate.m

#import "GuideViewController.h"
#import "MenuViewController.h"
#import "MeViewController.h"
#import "TopicViewController.h"


@interface AppDelegate ()

@end

@implementation AppDelegate
//私有方法,用于生成一个导航控制器
- (UINavigationController *)createNavigationWithController:(UIViewController *)vc title:(NSString *)title image:(NSString *)imgName selectImage:(NSString *)selectImgName{

    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    vc.navigationItem.title = title;
    nav.tabBarItem = [[UITabBarItem alloc]initWithTitle:title image:[UIImage imageNamed:imgName] selectedImage:[UIImage imageNamed:selectImgName]];


    return nav;
}


//标签栏控制器
- (UITabBarController *)tabBarCtl{
    if (!_tabBarCtl) {
        _tabBarCtl = [[UITabBarController alloc]init];
        self.tabBarCtl.tabBar.tintColor = [UIColor blackColor];
        UINavigationController *homeNav = [self createNavigationWithController:[[MenuViewController alloc]init] title:@"头条" image:@"main_bottom_tab_recipe_gray_25x25_" selectImage:@"main_bottom_tab_recipe_red_25x25_"];
        UINavigationController *selectNav = [self createNavigationWithController:[[TopicViewController alloc]init] title:@"小组" image:@"main_bottom_tab_pai_gray_25x25_" selectImage:@"main_bottom_tab_pai_red_25x25_"];

        UINavigationController *myNav = [self createNavigationWithController:[[MeViewController alloc]init] title:@"我的" image:@"main_bottom_tab_user_gray_25x25_" selectImage:@"main_bottom_tab_user_red_25x25_"];

        _tabBarCtl.viewControllers = @[homeNav,selectNav,myNav];

        [[UITabBar appearance] setTintColor:[UIColor colorWithRed:252/255.0  green:104/255.0  blue:106/255.0 alpha:1]];

        //设置导航的背景颜色
        [UINavigationBar appearance].barTintColor=[UIColor colorWithRed:252/255.0  green:104/255.0  blue:106/255.0 alpha:1];




    }
    return _tabBarCtl;
}




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


    GuideViewController *guideVC = [[GuideViewController alloc]init];
    self.vc = [[MenuViewController alloc]init];
    //第一次运行app
    if (![[NSUserDefaults standardUserDefaults]objectForKey:NOT_FIRST_LANUCH]) {
        self.window.rootViewController = guideVC;
    }
    //非首次运行app
    else{
        //获取当前的版本号

        //获取持久化的版本号数据
        NSString *savedVersion =  [[NSUserDefaults standardUserDefaults]objectForKey:NOT_FIRST_LANUCH];
        //判断版本号是否一致
        if ([savedVersion isEqualToString:VERSION_CURRENT]) {
            //没有更新
            self.window.rootViewController = self.vc;
        }
        else{
            //更新了
            self.window.rootViewController = guideVC;
        }
    }




    return YES;
}

GuideViewController.m

#import "ImageScrollView.h"
#import "AppDelegate.h"
@interface GuideViewController ()<ImageScrollViewDelegate>

@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    self.view.backgroundColor = [UIColor whiteColor];

    NSArray *imgArr = @[@"新手引导页5.jpg",@"新手引导页4.jpg",@"首页.jpg"];
    ImageScrollView *imgScrView  = [[ImageScrollView alloc]initWithFrame:CGRectNull style:ImageScrollType_Guide images:imgArr confirmBtnTitle:@"立即体验" confirmBtnTitleColor:[UIColor redColor] confirmBtnFrame:CGRectMake(230, 10, 100, 40) autoScrollTimeInterval:0 delegate:self];


    [self.view addSubview:imgScrView];
    [imgScrView addPageControlToSuperView:self.view];


}

#pragma mark -ImageScrollViewDelegate
- (void)experienceDidHandle{
    //获取当前的版本号,持久化
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:VERSION_CURRENT forKey:NOT_FIRST_LANUCH];
    [ud synchronize];

    //切换窗口的根视图控制器
    AppDelegate *app = App_Delegate;
    app.window.rootViewController = app.vc;

}

MenuViewController.m

  self.view.backgroundColor = [UIColor whiteColor];
        AppDelegate *app = App_Delegate;
        app.window.rootViewController = app.tabBarCtl;
相关标签: 界面