IOS 兩種控制器的使用,純程式碼UITabBarController 與 UINavigationController

來源:互聯網
上載者:User

標籤:mat   events      event   point   第三方   app   div   nsarray   

先說簡單的吧,UINavigationController代碼建立非常簡單,僅需一行代碼

//NewsViewController是你建立的一個ViewNewsViewController *newsPage = [[NewsViewController alloc]init];UINavigationController *newsNav = [[UINavigationController alloc] initWithRootViewController:newsPage];

然後是重頭戲,UITabBarController,我剛開始接觸這個控制器是在stroryboard裡面直接拖,非常簡單方便,但是後來在實際項目裡面這種方式是不適合的,需要用代碼來建立,我的需求是做一個引導頁,然後點擊引導頁上面的一個按鈕跳轉到UITabBarController頁面.遍查百度好像都是在AppDelegate.h這個裡面寫,我不喜歡在這個裡面寫,,,,,,終於找到了一種方式,非常簡單,一個第三方庫RDVTabBarController,廢話不多說,上代碼!!!

AppDelegate.h
//簡單的把viewController作為程式第一響應頁面- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // 修改導航顏色 [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:100 green:80 blue:50 alpha:1]]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; ViewController *vc = [[ViewController alloc] init]; self.window.rootViewController = vc; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;}

//繼承RDVTabBarController

ViewController.h#import "RDVTabBarController.h"@interface ViewController : RDVTabBarController@end

 

ViewController.m#import "ViewController.h"#import "RDVTabBarItem.h"#import "RDVTabBar.h"#import "RDVTabBarController.h"@interface ViewController ()@end@implementation ViewController{    // 初次登陸引導頁    UIScrollView *_firstLoginSV;}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];    NSInteger inter = [user integerForKey:@"isFirstLogin"];    if (!inter) {        self.navigationController.navigationBarHidden = YES;        self.rdv_tabBarController.tabBar.hidden = YES;        [self createFirstEnterImage];        [user setInteger:1 forKey:@"isFirstLogin"];    }else{        [self setUpViewControllers];    }                }// 初次登入APP引導頁- (void)createFirstEnterImage{    self.navigationController.navigationBarHidden = YES;    self.tabBarController.tabBar.hidden = YES;        _firstLoginSV = [[UIScrollView alloc] initWithFrame:self.view.frame];    _firstLoginSV.contentSize = CGSizeMake(3 * SCREEN_WIDTH, SCREEN_HEIGHT);    _firstLoginSV.backgroundColor = [UIColor yellowColor];    _firstLoginSV.pagingEnabled = YES;    _firstLoginSV.bounces = NO;    [self.view addSubview:_firstLoginSV];        NSArray *imageArray = @[@"back3",@"back4", @"back5"];    for (int i = 0; i < imageArray.count; i++) {        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];        imageView.image = [UIImage imageNamed:imageArray[i]];        [_firstLoginSV addSubview:imageView];    }        // 建立進入APP Button    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(2*SCREEN_WIDTH+(SCREEN_WIDTH - 150)/2, (SCREEN_HEIGHT - 100)/2, 150, 100)];    [btn setTitle:@"開始旅程" forState:UIControlStateNormal];    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];    [btn addTarget:self action:@selector(enterBtnClick) forControlEvents:UIControlEventTouchUpInside];    [_firstLoginSV addSubview:btn];}- (void)enterBtnClick {    [_firstLoginSV removeFromSuperview];    [self setUpViewControllers];    self.selectedIndex = 0;}//建立uitabar-(void)setUpViewControllers{        NewsViewController *newsPage = [[NewsViewController alloc]init];    UINavigationController *newsNav = [[UINavigationController alloc] initWithRootViewController:newsPage];        VideoViewController *preferentialPage = [[VideoViewController alloc] init];    UINavigationController *preferentialNav = [[UINavigationController alloc] initWithRootViewController:preferentialPage];    HappyViewController *happyPage = [[HappyViewController alloc] init];    UINavigationController *happyNav = [[UINavigationController alloc] initWithRootViewController:happyPage];    NSArray *items = @[newsNav, preferentialNav, happyNav];    self.viewControllers = items;    [self customizeTabBarForController:self];    }-(void)customizeTabBarForController:(RDVTabBarController *)tabBarController{    NSArray *tabBarItemTitle = @[@"每日新聞", @"不得其解", @"開心段子"];    NSArray *tabBarItemImages = @[@"news", @"shouye", @"duanzi"];    NSDictionary *selectedTitleAttributes = nil;    NSDictionary *unselectedTitleAttributes = nil;        self.rdv_tabBarController.tabBar.tintColor = [UIColor orangeColor];           NSInteger index = 0;    for (RDVTabBarItem *item in [[tabBarController tabBar] items]) {        // 設定 title        item.title = tabBarItemTitle[index];        // 擷取未選中圖片的名字        NSString *normalImageName = [NSString stringWithFormat:@"%@_normal.png", tabBarItemImages[index]];        // 擷取選中狀態圖片的名字        NSString *selectedImageName = [NSString stringWithFormat:@"%@_selected.png", tabBarItemImages[index]];        // 選中狀態 image        UIImage *selectedImage = [UIImage imageNamed:selectedImageName];        // 未選中狀態 image        UIImage *normalImage = [UIImage imageNamed:normalImageName];        // 設定 item 選中和未選中的表徵圖        [item setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:normalImage];        // 設定選中狀態 title 的字型大小和顏色        item.selectedTitleAttributes = selectedTitleAttributes;        // 設定未選中狀態 title 的字型大小和顏色        item.unselectedTitleAttributes = unselectedTitleAttributes;                index++;    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

   

 

IOS 兩種控制器的使用,純程式碼UITabBarController 與 UINavigationController

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.