iOS開發——代碼產生TabBar與視圖切換詳解
我在之前多篇部落格中講解了在不使用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來開發,也是極為方便的。