看到很多項目中都採用的是Navigation加Tab Bar組合到一起,完成視圖切換操作,在導覽列上添加基本按鈕,給予回應時間,讓應用給使用者更好的體驗,所以本菜鳥寫了這個這樣一個Demo,僅供學習
所建立工程模板是最後一個 Empty Application
先看運行效果:
第一個視圖,點擊按鈕切換視圖,點擊導覽列上按鈕可以切換回去
第二個視圖設定了背景顏色和透明度 第三個視圖添加了背景圖片
第四個視圖,在導覽列上添加了兩個按鈕,左邊按鈕屬於自訂標題,右邊按鈕是系統的表徵圖,點擊左按鈕彈出一個警告,右邊按鈕沒有添加響應事件,點擊後沒反應
Tab Bar上添加的都是自訂圖片
架構組合的主要代碼,在AppDelegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; _tabBarController = [[UITabBarController alloc] init]; _tabBarController.delegate = self; FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"secondViewController" bundle:nil]; ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil]; FourViewController *fourViewController = [[FourViewController alloc] initWithNibName:@"FourViewController" bundle:nil]; UINavigationController *navFirst = [[UINavigationController alloc] initWithRootViewController:firstViewController];// 在載入圖片是把標題都覆蓋了,所以運行效果並沒有顯示這些文字 navFirst.title = @"第一個視圖"; UINavigationController *navSecond = [[UINavigationController alloc] initWithRootViewController:secondViewController]; navSecond.title = @"第二個視圖"; UINavigationController *navThird = [[UINavigationController alloc] initWithRootViewController:thirdViewController]; navThird.title = @"第三個視圖"; UINavigationController *navFour = [[UINavigationController alloc] initWithRootViewController:fourViewController]; navFour.title = @"第四個視圖"; _tabBarController.viewControllers = [NSArray arrayWithObjects:navFirst,navSecond,navThird,navFour, nil]; _window.rootViewController = _tabBarController; [self.window addSubview:firstViewController.view]; [self.window makeKeyAndVisible]; return YES;}
第一個視圖切換按鈕響應事件
- (IBAction)switchView:(id)sender { FirstViewController *firstController = [[FirstViewController alloc] init]; [self.navigationController pushViewController:firstController animated:YES]; firstController.title = @"第一個視圖另一個視圖";}
第四個視圖添加兩個按鈕方法,在最後一個控制機的.m檔案中的-(void)viewDidLoad方法中
- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view. UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"測試" style:UIBarButtonItemStylePlain target:self action:@selector(pullWarn)]; self.navigationItem.leftBarButtonItem = leftButton; UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil]; self.navigationItem.rightBarButtonItem = rightButton;}
原始碼:http://download.csdn.net/detail/duxinfeng2010/4576308