ios項目常用模板架構之UITabBar+Nav
在實際的項目開發中總是有幾個比較常見的模板,小編這幾天給大夥出幾期常用模板的部落格,希望大家多提寶貴的意見! 這幾個月最常用的莫過於Nav+UITabBar模板了;在實際的項目中,我比較側重於純程式碼,比較不喜歡拖控制項,至於利弊在這裡不多說了,言歸正傳。
首先在AppDelegate.m中建立一個空白布景:
self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
初始化UITabBarController
self.tabBarController=[[UITabBarController alloc]init];
建立兩個視圖控制器:
FirstViewController *one=[[FirstViewController alloc]init];
SecondViewController *second=[[SecondViewController alloc]init];
建立兩個導航控制器並 讓這兩個導航控制器控制好各自的視圖控制器:
UINavigationController *navFirst=[[UINavigationController alloc]initWithRootViewController:one];
UINavigationController *navSecond=[[UINavigationController alloc]initWithRootViewController:second];
讓tabBarController包含這兩個導航控制器:
[self.tabBarController addChildViewController:navFirst];
[self.tabBarController addChildViewController:navSecond];
對各自的視圖控制器進行細膩化的定製:
one.title=@”連絡人”;
one.tabBarItem=[[UITabBarItem alloc]initWithTitle:@”one” image:[UIImage imageNamed:@”“]
selectedImage:nil];
second.title=@”收藏”;
second.tabBarItem=[[UITabBarItem alloc]initWithTitle:@”second” image:nil selectedImage:nil];
設定改空白布景上的主視圖為:tabBarController:
[self.tabBarController addChildViewController:navSecond];
修改布景為紅色:
self.window.backgroundColor=[UIColor redColor];
顯示布景:
[self.window makeKeyAndVisible];
在FirstViewController中建立二級頁面
建立導覽列:
UIBarButtonItem *rightButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(selectRightAction:) ];
實現跳轉方法:
-(void)selectRightAction:(id)sender
{
BackViewController *backButton;
backButton=[[BackViewController alloc]initWithNibName:@BackViewController bundle:nil];backButton.title=@第二視圖層;[self.navigationController pushViewController:backButton animated:NO];
}