您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發:純程式碼生成 TabBar 與視圖切換詳解

編輯:iOS 開發綜合

我在之前的多篇部落格中,講解了在不使用 storyboard、而採用 nib 檔的情況下,如何用程式碼生成導航欄並進行跳轉,具體可參考《iOS 開發——界面跳轉與返回及視圖類型詳解》《iOS 純代碼實現界面建立、跳轉、導航欄(無 storyboard、無 nib)(Objective-C)》。今天我要說明的是:在使用 nib 搭建界面的前提下,如何用程式碼生成 TabBar,並在視圖之間進行跳轉。示例程式碼已上傳至:https://github.com/chenyufeng1991/TabBarTest 。

(1)在此示例中,Navigation 與 TabBar 都會透過程式碼來實作,因此需要先在 AppDelegate 中進行初始化設定,如下。其中 RootViewController 是後面才定義的一個根視圖。

 #import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //聲明根視圖; RootViewController *root = [[RootViewController alloc]init]; self.window.rootViewController = root; [self.window makeKeyAndVisible]; return YES; } @end

(2)RootViewController 定義了根視圖,在這裡定義了頁面的 Navigation 與 TabBar,這是我們第一個看到的視圖。

 #import "RootViewController.h" #import "FirstViewController.h" #import "SecondViewController.h" @interface RootViewController () //聲明TabBar @property (nonatomic,strong)UITabBarController *tabBarController; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; UITabBarController *tabBarController = [[UITabBarController alloc]init]; tabBarController.delegate = self; /** 把兩個界面加入到根視圖中; 兩個界面也分別要導航欄; */ FirstViewController *firstVC = [[FirstViewController alloc]init]; UINavigationController *firstNav = [[UINavigationController alloc]initWithRootViewController:firstVC]; firstNav.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:0]; SecondViewController *secondVC = [[SecondViewController alloc]init]; UINavigationController *secondNav = [[UINavigationController alloc]initWithRootViewController:secondVC]; secondNav.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1]; //通過數組存儲; tabBarController.viewControllers = [NSArray arrayWithObjects:firstNav,secondNav, nil]; self.tabBarController = tabBarController; [self.view addSubview:tabBarController.view]; } @end

(3)TabBar 的第一個 Tab 實作如下,我在這裡透過一個按鈕以 push 方式跳到另一個頁面(該頁同樣會出現導航欄與 TabBar)。

 #import "FirstViewController.h" #import "First02ViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"1111"; } - (IBAction)buttonPressed:(id)sender { //通過push跳到另一個界面; First02ViewController *first02 = [[First02ViewController alloc] init]; [self.navigationController pushViewController:first02 animated:true]; } @end

(4)在上述 push 到另一個界面後,可以使用導航欄自帶的「返回」按鈕返回,也可以透過 pop 返回:

 #import "First02ViewController.h" @interface First02ViewController () @end @implementation First02ViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"新聞"; } - (IBAction)backButtonPressed:(id)sender { //通過pop返回到push過來的界面; [self.navigationController popViewControllerAnimated:true]; } @end

(5)在第二個 Tab 中,我透過點擊按鈕以 Modal 方式跳轉到另一個頁面(該頁面沒有導航欄,也沒有 TabBar)。

 #import "SecondViewController.h" #import "Second02ViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"2222"; } - (IBAction)buttonPressed:(id)sender { //通過modal方式跳轉,跳過去後的界面沒有導航欄; Second02ViewController *second02 = [[Second02ViewController alloc] init]; [self presentViewController:second02 animated:true completion:nil]; } @end

然後透過 dismiss 返回。

 #import "Second02ViewController.h" @interface Second02ViewController () @end @implementation Second02ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)backButtonPressed:(id)sender { //通過dismiss返回modal過來的界面; [self dismissViewControllerAnimated:true completion:nil]; } @end

直接看上面的程式碼可能會覺得有點亂,你可以下載原始碼執行後再對照查看。這套寫法也可以直接作為界面架構來使用;而如果你想改用 storyboard 來開發,同樣十分方便。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved