貓貓學iOS 之微博項目實戰(1)微博主架構-子控制器的添加,ios實戰
貓貓分享,必須精品
原創文章,歡迎轉載。轉載請註明:翟乃玉的部落格
地址:http://blog.csdn.net/u013357243?viewmode=contents
一:簡單介紹
這是新浪微博的iOS端項目,來自於黑馬的一個實戰項目。(本人沒有培訓,純屬自學,但人要學會感恩,雖然是自己買的學習資料,但是飲水思源!!)
主要分成五大模組,本次全部運用純程式碼實現,其中會用到很多前面學過得內容,如果有的地方有重複的知識點,說明這個知識點真的很重要,沒有時間看視頻學習或者培訓的朋友們,可以看貓貓的這一系列部落格,貓貓會竭盡所能的完善他。
有什麼不全面的地方,歡迎大家回複我,貓貓會儘快改正的。
二:建立項目匯入素材
第一步首先我們要建立我們的項目了,在這兒我並沒有用最新版的xcode6,而是用的xcode5.1,為什麼用的5.1而不是6呢?
首先:我所學習的視頻是用的xcode5.1,這是最主要的原因,貓貓作為一個體育生,自學編程兩年所得到的經驗就是,在看視頻自學的時候儘可能讓自己的一切與別人實體教學一樣,貓貓曾經在學android時候就因為android 的sdk不一樣,新出的片段化跟視頻中的操作不一樣而大大打擊學習的積極性。
其次:企業還有很多再用5.1甚至是4.1(據朋友說。。。)當然,這個也可以看出主要原因啦。不過貓貓建議,在學到一定水平的時候,多接收下新的知識,當然我覺得舊的更重要,不說咱國語有云,溫故知新,萬變不離其宗,學紮實舊的知識,新的知識上手搜easy。
建立項目
選擇Single View Application 選擇下一步
寫入項目名稱貓貓微博(名字隨便) 選擇下一步
然後就是下一步下一步了
因為我們要用純程式碼,所以先刪除其他沒用的Controller還有Main.storyboard
Remove References 表示只是在xcode中刪除,但是在檔案中還有。
Move To Trash 就代表,直接進入了系統廢紙簍了
然後匯入素材(我們用到的圖片) images.xcassets
applcon裡面的是在手機首頁的那個圖
launchimage是指得應用載入時候開始的那個圖片
對於第一個圖片的空白位置,我們可以直接通過修改json檔案來控制,當然,直接拖拽進去也可以(相當於修改json)
三:建立window
前面有部落格寫過程式運行時候會經過哪些步驟。
因為我們刪除了Main.storyboard,這時候運行程式會報錯,我們需要修改圖裡面的main 刪除他,改成空
這時候運行程式,將會是這樣:
烏七八黑。。。
然後我們要在NYAppDelegate.m來寫代碼了。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ //1.建立視窗 self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; //2.設定根控制器 NYTabBarViewController *tabbarC = [[NYTabBarViewController alloc] init]; self.window.rootViewController = tabbarC; //3.顯示視窗 [self.window makeKeyAndVisible]; return YES;}
簡單三步,就可以顯示了。
自己寫NYTabBarViewController.m
//// NYTabBarViewController.m// 貓貓微博//// Created by apple on 15-6-2.// Copyright (c) 2015年 znycat. All rights reserved.//#import "NYTabBarViewController.h"#import "NYHomeViewController.h"#import "NYMessageCenterViewController.h"#import "NYDiscoverViewController.h"#import "NYProfileViewController.h"#import "NYNavigationController.h"@interface NYTabBarViewController ()@end@implementation NYTabBarViewController-(void)viewDidLoad{ [super viewDidLoad]; // 1.初始化子控制器 // 1.初始化子控制器 NYHomeViewController *home = [[NYHomeViewController alloc] init]; [self addChildVc:home title:@"首頁" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"]; NYMessageCenterViewController *messageCenter = [[NYMessageCenterViewController alloc] init]; [self addChildVc:messageCenter title:@"訊息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"]; NYDiscoverViewController *discover = [[NYDiscoverViewController alloc] init]; [self addChildVc:discover title:@"發現" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"]; NYProfileViewController *profile = [[NYProfileViewController 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] = NYColor(123, 123, 123); NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary]; selectTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor]; [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal]; [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected]; // 先給外面傳進來的小控制器 封裝 一個導航控制器 NYNavigationController *nav = [[NYNavigationController alloc] initWithRootViewController:childVc]; // 添加為子控制器 [self addChildViewController:nav];}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
這裡用到了物件導向的思路來設計代碼,我們需要一個NYTabBarViewController,要建立他,誰最清楚呢?當然是他自己了,所以我們就把代碼放到他自己裡面。做好代碼的重構。
而對於
“NYHomeViewController.h”
“NYMessageCenterViewController.h”
“NYDiscoverViewController.h”
“NYProfileViewController.h”
“NYNavigationController.h”
這些,大家先把他看做是tableViewController或者ViewController就可以了,就是一個個的類,還沒有實現功能。
效果就是這樣: