iOS開發項目篇—02添加子控制器以及項目分層

來源:互聯網
上載者:User

標籤:

iOS開發項目篇—02添加子控制器以及項目分層

一、添加子控制器

1.設定根控制器(自訂)

說明:分析新浪微博應用,觀察其整體建構層次。而系統的控制器不能滿足項目開發的需求,這裡把項目中原有的控制器刪除.自己定義一個TabBarViewController類。讓這個類作為window視窗的根控制器。

YYAppDelegate.m檔案代碼:

 1 #import "YYAppDelegate.h" 2 #import "YYTabBarViewController.h" 3  4 @implementation YYAppDelegate 5  6 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 7 { 8   9    //1.建立視窗10     self.window=[[UIWindow alloc]init];11     self.window.frame=[UIScreen mainScreen].bounds;12     13     //2.設定視窗的根控制器14 //    UITabBarController *tabbarVc =[[UITabBarController alloc]init];15 //    self.window.rootViewController=tabbarVc;16     self.window.rootViewController=[[YYTabBarViewController alloc]init];17     18     //3.顯示視窗19     [self.window makeKeyAndVisible];20     return YES;21 }22         

2.建立四個自訂的控制器

說明:根據功能模組劃分,把該項目劃分為四個大的部分,分別是首頁、訊息、發現和“我”,根據項目需要,自訂四個子控制器,對這四個模組分別進行管理。

自訂四個控制器,讓其繼承自UITableViewController

在中控制器 (YYTabBarViewController)中,添加四個子控制器

3.拷貝需要的圖片素材到項目中

建議:在拷貝圖片的時候,建議使用硬碟對硬碟的拷貝,即在finder中進行。

4.修改系統外掛程式

在項目中,經常會使用到分類(如本項目中使用了一個自己定義UIImage的匪類),但是使用這種分類的方法時,我們安裝的只能提示外掛程式可能並不會有智能提示,那麼這種情況下可以嘗試修改外掛程式。

(1)找到外掛程式在XCode中的安裝路徑

提示:Xcode的外掛程式安裝路徑: /Users/使用者名稱/Library/Application Support/Developer/Shared/Xcode/Plug-ins

(2)顯示包內容

(3)修改plist檔案

二、實現代碼

YYTabBarViewController.m檔案

 1 // 2 //  YYTabBarViewController.m 3 //  02-微博添加子控制器和設定項目結構 4 // 5 //  Created by apple on 14-7-3. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYTabBarViewController.h"10 #import "YYHomeTableViewController.h"11 #import "YYDiscoverViewController.h"12 #import "YYMessageViewController.h"13 #import "YYProfileViewController.h"14 #import "UIImage+Extension.h"15 16 @interface YYTabBarViewController ()17 18 @end19 20 @implementation YYTabBarViewController21 22 23 - (void)viewDidLoad24 {25     [super viewDidLoad];26     //添加四個子控制器27     YYHomeTableViewController *home=[[YYHomeTableViewController alloc]init];28     [self addOneChildVc:home title:@"首頁" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"];29     30     31     YYMessageViewController *message=[[YYMessageViewController alloc]init];32     [self addOneChildVc:message title:@"訊息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"];33     34     YYDiscoverViewController *discover=[[YYDiscoverViewController alloc]init];35     [self addOneChildVc:discover title:@"發現" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"];36     37     YYProfileViewController *profile=[[YYProfileViewController alloc]init];38     [self addOneChildVc:profile title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"];39 }40 41 /**42  *  添加一個子控制器43  *44  *  @param childVC           子控制對象45  *  @param title             標題46  *  @param imageName         表徵圖47  *  @param selectedImageName 選中時的表徵圖48  */49 -(void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName50 {51     //隨機設定子控制器的背景顏色52     childVc.view.backgroundColor=YYRandomColor;53     //設定標題54     childVc.tabBarItem.title=title;55     //設定表徵圖56     childVc.tabBarItem.image=[UIImage imageWithName:imageName];57     //設定選中時的表徵圖58     UIImage *selectedImage=[UIImage imageWithName:selectedImageName];59     60     61     if (iOS7) {62         // 聲明這張圖片用原圖(別渲染)63         selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];64     }65     childVc.tabBarItem.selectedImage = selectedImage;66     67     68     //添加子控制器到tabbar69     [self addChildViewController:childVc];70 }71 72 73 // 在iOS7中, 會對selectedImage的圖片進行再次渲染為藍色74 // 要想顯示原圖, 就必須得告訴它: 不要渲染75 76 // Xcode的外掛程式安裝路徑: /Users/使用者名稱/Library/Application Support/Developer/Shared/Xcode/Plug-ins77 @end

UIImage分類

UIImage+Extension.h檔案

 1 // 2 //  UIImage+Extension.h 3 //   4 // 5 //  Created by apple on 14-7-3. 6 //  Copyright (c) 2014年 heima. All rights reserved. 7 // 8  9 #import <UIKit/UIKit.h>10 11 @interface UIImage (Extension)12 + (UIImage *)imageWithName:(NSString *)name;13 @end

UIImage+Extension.m檔案

 1 // 2 //  UIImage+Extension.m 3 //   4 // 5 //  Created by apple on 14-7-3. 6 //  Copyright (c) 2014年 heima. All rights reserved. 7 // 8  9 #import "UIImage+Extension.h"10 11 @implementation UIImage (Extension)12 + (UIImage *)imageWithName:(NSString *)name13 {14     UIImage *image = nil;15     if (iOS7) { // 處理iOS7的情況16         NSString *newName = [name stringByAppendingString:@"_os7"];17         image = [UIImage imageNamed:newName];18     }19     20     if (image == nil) {21         image = [UIImage imageNamed:name];22     }23     return image;24 }25 @end

在設定檔中的宏定義

 1 // 2 //  Prefix header 3 // 4 //  The contents of this file are implicitly included at the beginning of every source file. 5 // 6  7 #import <Availability.h> 8  9 #ifndef __IPHONE_5_010 #warning "This project uses features only available in iOS SDK 5.0 and later."11 #endif12 13 #ifdef __OBJC__14     #import <UIKit/UIKit.h>15     #import <Foundation/Foundation.h>16 17 // 隨機色18 #define YYRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]19 20 // 是否為iOS721 #define iOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0)22 #endif

運行情況:

       

三、規劃專案檔結構

該項目的開發以模組進行劃分,檔案結構如下:

提示:在進行結構劃分的時候,可以在項目中建立分組,分組完成後,把相應的檔案拖到對應的組中,但分組是虛擬資料夾,通過查看代碼的檔案可以發現,在硬碟上實質上是沒有分檔案夾的。

建議:直接在硬碟中進行分組,以解決這個問題。

iOS開發項目篇—02添加子控制器以及項目分層

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.