标签:
我在之前多篇博客中讲解了在不使用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 ()<UITabBarControllerDelegate> //声明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来开发,也是极为方便的。
github主页:https://github.com/chenyufeng1991 。欢迎大家访问!
最近极客学院Wiki正在进行IT职业技能图谱的制定,我主要负责iOS方向,大家感兴趣的可以一起参加,有问题或者修改可以直接给我发issues或者pull request。https://github.com/chenyufeng1991/skillmap 。
标签:
原文地址:http://blog.csdn.net/chenyufeng1991/article/details/50380977