IOS第一天-新浪微博 - 框架的搭建

*************HWAppDelegate.mapp

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 1.建立窗口
    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;
    
    // 2.設置根控制器
    self.window.rootViewController = [[HWTabBarViewController alloc] init];
    
    // 4.顯示窗口
    [self.window makeKeyAndVisible];
    return YES;
}
*******HWTabBarViewController.m
#import "HWTabBarViewController.h"
#import "HWHomeViewController.h"
#import "HWMessageCenterViewController.h"
#import "HWDiscoverViewController.h"
#import "HWProfileViewController.h"
#import "HWNavigationController.h"

@interface HWTabBarViewController ()

@end

@implementation HWTabBarViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 1.初始化子控制器
    HWHomeViewController *home = [[HWHomeViewController alloc] init];
    [self addChildVc:home title:@"首頁" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
    
    HWMessageCenterViewController *messageCenter = [[HWMessageCenterViewController alloc] init];
    [self addChildVc:messageCenter title:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
    
    HWDiscoverViewController *discover = [[HWDiscoverViewController alloc] init];
    [self addChildVc:discover title:@"發現" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
    
    HWProfileViewController *profile = [[HWProfileViewController alloc] init];
    [self addChildVc:profile title:@"" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
}

/**
 *  添加一個子控制器
 *
 *  @param childVc       子控制器
 *  @param title         標題
 *  @param image         圖片
 *  @param selectedImage 選中的圖片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 設置子控制器的文字
    childVc.title = title; // 同時設置tabbar和navigationBar的文字
//    childVc.tabBarItem.title = title; // 設置tabbar的文字
//    childVc.navigationItem.title = title; // 設置navigationBar的文字
    
    // 設置子控制器的圖片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 設置文字的樣式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = HWColor(123, 123, 123);
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    childVc.view.backgroundColor = HWRandomColor;   //設置了 這個會實例化 view
    
    // 先給外面傳進來的小控制器 包裝 一個導航控制器
    HWNavigationController *nav = [[HWNavigationController alloc] initWithRootViewController:childVc];
    // 添加爲子控制器
    [self addChildViewController:nav];
}

@end

************HWTabBarViewController.h(導航控制器)dom

#import <UIKit/UIKit.h>

@interface HWTabBarViewController : UITabBarController

@end

***********HWNavigationController.mide

#import "HWNavigationController.h"

@interface HWNavigationController ()

@end

@implementation HWNavigationController

+ (void)initialize
{
    // 設置整個項目全部item的主題樣式
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    
    // 設置普通狀態
    // key:NS****AttributeName
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    // 設置不可用狀態
    NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
    disableTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.7];
    
    //  每個像素都有本身的顏色,每一種顏色均可以由RGB3色組成
    //  12bit顏色: #f00  #0f0 #00f #ff0
    //  24bit顏色: #ff0000 #ffff00  #000000  #ffffff
    
    // #ff ff ff
    // R:255
    // G:255
    // B:255
    
    // RGBA
    //  32bit顏色: #556677
    
    // #ff00ff
    
    disableTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
}

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  重寫這個方法目的:可以攔截全部push進來的控制器
 *
 *  @param viewController 即將push進來的控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) { // 這時push進來的控制器viewController,不是第一個子控制器(不是根控制器)
        /* 自動顯示和隱藏tabbar */
        viewController.hidesBottomBarWhenPushed = YES;
        
        /* 設置導航欄上面的內容 */
        // 設置左邊的返回按鈕
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(back) image:@"navigationbar_back" highImage:@"navigationbar_back_highlighted"];
        
        // 設置右邊的更多按鈕
        viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(more) image:@"navigationbar_more" highImage:@"navigationbar_more_highlighted"];
    }

    [super pushViewController:viewController animated:animated];
}

- (void)back
{
#warning    這裏要用self,不是self.navigationController
    // 由於self原本就是一個導航控制器,self.navigationController這裏是nil的
    [self popViewControllerAnimated:YES];
}

- (void)more           //返回到跟控制器
{
    [self popToRootViewControllerAnimated:YES];
}
@end

************HWHomeViewController.m測試

#import "HWHomeViewController.h"

@interface HWHomeViewController ()

@end

@implementation HWHomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    /* 設置導航欄上面的內容 */
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(friendSearch) image:@"navigationbar_friendsearch" highImage:@"navigationbar_friendsearch_highlighted"];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(pop) image:@"navigationbar_pop" highImage:@"navigationbar_pop_highlighted"];
}

- (void)friendSearch
{
    NSLog(@"friendSearch");
}

- (void)pop
{
    NSLog(@"pop");
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0;
}

@end
HWHomeViewController.h
#import <UIKit/UIKit.h>

@interface HWHomeViewController : UITableViewController

@end

************HWMessageCenterViewController.m (消息) this

#import "HWMessageCenterViewController.h"
#import "HWTest1ViewController.h"

@interface HWMessageCenterViewController ()

@end

@implementation HWMessageCenterViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // style : 這個參數是用來設置背景的,在iOS7以前效果比較明顯, iOS7中沒有任何效果
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"寫私信" style:UIBarButtonItemStylePlain target:self action:@selector(composeMsg)];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 這個item不能點擊(目前放在viewWillAppear就能顯示disable下的主題)
    self.navigationItem.rightBarButtonItem.enabled = NO;
}

- (void)composeMsg
{
    NSLog(@"composeMsg");
}

#pragma mark - Table view data sourc
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"test-message-%d", indexPath.row];
    
    return cell;
}

#pragma mark - 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWTest1ViewController *test1 = [[HWTest1ViewController alloc] init];
    test1.title = @"測試1控制器";
    // 當test1控制器被push的時候,test1所在的tabbarcontroller的tabbar會自動隱藏
    // 當test1控制器被pop的時候,test1所在的tabbarcontroller的tabbar會自動顯示
//    test1.hidesBottomBarWhenPushed = YES;
    
    // self.navigationController === HWNavigationController
    [self.navigationController pushViewController:test1 animated:YES];
}
@end

 

************HWMessageCenterViewController.h (消息)spa

#import <UIKit/UIKit.h>

@interface HWMessageCenterViewController : UITableViewController

@end

**********HWDiscoverViewController.m(發現)代理

#import "HWDiscoverViewController.h"

@interface HWDiscoverViewController ()

@end

@implementation HWDiscoverViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

@end

**********HWDiscoverViewController.h(發現)code

#import <UIKit/UIKit.h>

@interface HWDiscoverViewController : UITableViewController

@end

HWProfileViewController.m(我)orm

#import "HWProfileViewController.h"
#import "HWTest1ViewController.h"

@interface HWProfileViewController ()

@end

@implementation HWProfileViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"設置" style:0 target:self action:@selector(setting)];
//    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(setting) image:@"navigationbar_pop" highImage:@"navigationbar_pop_highlighted"];
}

- (void)setting
{
    HWTest1ViewController *test1 = [[HWTest1ViewController alloc] init];
    test1.title = @"test1";
    [self.navigationController pushViewController:test1 animated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0;
}
 

@end

***************HWProfileViewController.h(我)blog

#import <UIKit/UIKit.h>

@interface HWProfileViewController : UITableViewController

@end
相關文章
相關標籤/搜索