我在之前多篇博客中講解了在不使用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
#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
#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
#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來開發,也是極為方便的。