扯一下自己對純代碼的看法
從我開始接觸iOS開發以來,就是一直采用的純代碼手寫,俗稱肉碼。這個當時也是因為項目組里面當時的組長的要求,說是這樣便于大家的協同開發,不過后面確實也印證了這一說法,純代碼開發確實對像我一樣的菜鳥來說,更容易看懂一些。
雖然現在,我也還在堅持純代碼編寫工程,但是確實也看到的一些不妥,效率相對較低,而且Apple出了Interface Builder 工具,為什么不嘗試呢,所以今后也會向IB開進,學習。
Xcode創建一個single project
Snip20160326_12.png
然后進入工程,將main.stroreboard刪除掉
在工程配置文件里 將Main Interface清空,這里需要注意 刪除掉后,需要保存 最好運行一次,不然會刪除失效的
Snip20160326_14.png
然后找到Applegate.m在 didFinishLaunchingWithOptions 方法寫程序入口 一般我的習慣是創建一個main tab viewController 用來管理幾個tab 當然這也是看需求的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
YQPPMainViewController *vc = [[YQPPMainViewController alloc] init];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
這樣程序的入口就變成了YQPPMainViewController這個viewcontroler 然后可以在這個tabcontroller里面加入一些子controller等
- (void)viewDidLoad {
[super viewDidLoad];
_firstVC = [[YQPPFirstViewController alloc] init];
[self addChidVC:_firstVC title:@"首頁" image:[UIImage imageNamed:@"first_normal"] selectImage:[UIImage imageNamed:@"first_select"]];
_secondVC = [[YQPPSecondViewController alloc] init];
[self addChidVC:_secondVC title:@"二頁" image:[UIImage imageNamed:@"second_normal"] selectImage:[UIImage imageNamed:@"second_select"]];
_thirdVC = [[YQPPThirdViewController alloc] init];
[self addChidVC:_thirdVC title:@"三頁" image:[UIImage imageNamed:@"third_normal"] selectImage:[UIImage imageNamed:@"third_select"]];
_fourthVC = [[YQPPFourthViewController alloc] init];
[self addChidVC: _fourthVC title: @"四頁" image:[UIImage imageNamed:@"fourth_normal"] selectImage: [UIImage imageNamed:@"fourth_select"]];
}
/**
* 添加子控制器
*
* @param viewController 控制器
* @param title tabbar的title
* @param image 未選中的圖片
* @param selectImage 選中的圖片
*/
- (void)addChidVC:(UIViewController *)viewController title:(NSString *)title image:(UIImage *)image selectImage:(UIImage *)selectImage {
//設置圖標和標題
viewController.tabBarItem.title = title;
viewController.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
viewController.tabBarItem.selectedImage = [selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[viewController.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor],NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
//包裝一個navigationvc
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:viewController];
[self addChildViewController:nvc];
}
每一個tabbar對應的controller都能夠清晰展示出來