美文网首页
UITabBarController- 使用

UITabBarController- 使用

作者: 奇梦人 | 来源:发表于2020-03-22 22:42 被阅读0次
    image.png
    
    //
    //  AppDelegate.m
    //  SimpleApp
    //
    //  Created by Wenshuo on 2020/3/22.
    //  Copyright © 2020年 Wenshuo. All rights reserved.
    //
    
    #import "AppDelegate.h"
    #import "ViewController.h"
    @interface AppDelegate ()<UITabBarControllerDelegate>
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        //设置 UIWindow
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds] ];
        // 底部tabbar
        UITabBarController *tabbar = [[UITabBarController alloc] init];
        // 管理 UIViewController 的跳转
        UINavigationController *uinavigatController =[[UINavigationController alloc] initWithRootViewController:tabbar];
        
        
        ViewController  *viewController =[[ViewController alloc] init];
        viewController.view.backgroundColor = [UIColor greenColor];
        viewController.tabBarItem.title = @"新闻";
        
        UIViewController  *viewController2 =[[UIViewController alloc] init];
         viewController2.view.backgroundColor = [UIColor redColor];
        viewController2.tabBarItem.title = @"资讯";
       
        UIViewController  *viewController3 =[[UIViewController alloc] init];
         viewController3.view.backgroundColor = [UIColor yellowColor];
        viewController3.tabBarItem.title = @"关注";
     
        
        UIViewController  *viewController4 =[[UIViewController alloc] init];
         viewController4.view.backgroundColor = [UIColor blueColor];
        viewController4.tabBarItem.title = @"我的";
    //    viewController4.tabBarItem.image = [UIImage imageNamed:@"icon"];
    //    viewController4.tabBarItem.selectedImage = [UIImage imageNamed:@""];
        
        [tabbar setViewControllers:@[viewController,viewController2,viewController3,viewController4]];
        // 可以理解为 tabbar 选项卡的监听
        tabbar.delegate = self;
        //如果要底部不出现 tabbar 必须设置 rootViewController 为 uinavigatController
        self.window.rootViewController = uinavigatController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    // 配合tabbar.delegate = self;  当点击tabbar 会回调此方法
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
        NSLog(@"did select");
    }
    
    
    
    - (void)applicationWillResignActive:(UIApplication *)application {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }
    
    
    - (void)applicationDidEnterBackground:(UIApplication *)application {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    
    - (void)applicationWillEnterForeground:(UIApplication *)application {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }
    
    
    - (void)applicationDidBecomeActive:(UIApplication *)application {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    
    - (void)applicationWillTerminate:(UIApplication *)application {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    
    @end
    
    
    

    相关文章

      网友评论

          本文标题:UITabBarController- 使用

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