自學iOS開發系列----UI(視圖編程入門:UITabBarController)

本章教學效果:
HTTabBarController

核心代碼
封裝工具類Tools:
HTTools.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface HTTools : NSObject

//工廠模式:想要創建一個Button
+ (UIButton *)createButton:(CGRect)frame bgColor:(UIColor *)bgColor title:(NSString *)title titleColor:(UIColor *)titleColor tag:(NSInteger)tag action:(SEL)action vc:(id)vc;

//創建標籤
+ (UILabel *)createLabel:(CGRect)frame text:(NSString *)text textAlignment:(NSTextAlignment)textAlignment textColor:(UIColor *)textColor bgColor:(UIColor *)bgColor tag:(NSInteger)tag;

@end

HTTools.m

#import "HTTools.h"

@implementation HTTools

+ (UIButton *)createButton:(CGRect)frame bgColor:(UIColor *)bgColor title:(NSString *)title titleColor:(UIColor *)titleColor tag:(NSInteger)tag action:(SEL)action vc:(id)vc {
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    button.backgroundColor = bgColor;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.tag = tag;
    [button addTarget:vc action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (UILabel *)createLabel:(CGRect)frame text:(NSString *)text textAlignment:(NSTextAlignment)textAlignment textColor:(UIColor *)textColor bgColor:(UIColor *)bgColor tag:(NSInteger)tag {
    UILabel * label = [[UILabel alloc] init];
    label.frame = frame;
    label.text = text;
    label.textAlignment = textAlignment;
    label.textColor = textColor;
    label.backgroundColor = bgColor;
    label.tag = tag;
    return label;
}

@end

AppDelegate.m

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];

    /**
     *  標籤欄控制器UITabBarController
     *  標籤欄控制器的工作原理和導航控制器一樣,都是用來管理子視圖控制器之間的層次關係
     *
     *   不同點:導航控制器使用的是壓棧和出棧,但是標籤欄控制器上的所有子視圖控制器層次上是平級的,就是說所有的子視圖控制器平鋪在標籤欄控制器之上
     */
    RootViewController * root = [[RootViewController alloc] init];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:root];
    //爲root上的導航條添加標題,導航條上的內容必須通過導航控制器上的子視圖控制器對象調用navigationItem的屬性設置
    root.navigationItem.title = @"首頁";
    //同樣的爲標籤欄控制器上的視圖添加標題 使用標籤欄控制器上的子視圖控制器對象調用tabBarItem.title
    nav.tabBarItem.title = @"界面1";

    SecondViewController * second = [[SecondViewController alloc] init];
    second.tabBarItem.title = @"界面2";

    ThirdViewController * third = [[ThirdViewController alloc] init];
    third.tabBarItem.title = @"界面3";

    FourViewController * four = [[FourViewController alloc] init];
    four.tabBarItem.title = @"界面4";

    //創建標籤欄控制器對象
    UITabBarController * tabBar = [[UITabBarController alloc] init];
    //向tabBarController上添加子視圖控制器
    tabBar.viewControllers = @[nav,second,third,four];
    self.window.rootViewController = tabBar;
    [self.window makeKeyAndVisible];

demo地址

發佈了54 篇原創文章 · 獲贊 83 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章