TabBar实现页面跳转(AppDelegate +NavigationViewController + TabBarViewController)
2015-08-18 11:58
531 查看
1. AppDelegate.h
定义TabBarController
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBarController;
@end
2.AppDelegate.m
#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface AppDelegate ()<UITabBarControllerDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];//实例化window
self.window.backgroundColor = [UIColor whiteColor];//设置window背景
_tabBarController = [[UITabBarController alloc]init]; //实例化选项卡控制器
_tabBarController.delegate = self; //将AppDelegate设置成选项卡控制器的代理???
//实例化3个控制器
FirstViewController *firstViewController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *secondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
ThirdViewController *thirdViewController = [[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil];
//实例化3个导航器,将每个控制器的对象都放到导航器的跟控制器的方法中
UINavigationController *navFirst = [[UINavigationController alloc]initWithRootViewController:firstViewController];
navFirst.title = @"花时间";
UINavigationController *navSecond = [[UINavigationController alloc]initWithRootViewController:secondViewController];
navSecond.title = @"花会友";
UINavigationController *navThird = [[UINavigationController alloc]initWithRootViewController:thirdViewController];
navThird.title = @"我";
//将导航器对象都放到选项卡的子控制器(viewcontroller)中
_tabBarController.viewControllers = [NSArray arrayWithObjects:navFirst,navSecond,navThird, nil];
//设置UIWindow的rootViewController为UITabBarController
_window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
3.每个UIViewController都设置一下TabBarItem的标题
FirstViewController.m
#import "FirstViewController.h"
#import "AppDelegate.h"
@interface FirstViewController ()<UITableViewDataSource,UITableViewDelegate>{
NSMutableArray *_allFlowers;
}
@end
@implementation FirstViewController
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.image = [UIImage imageNamed:@"first.png"];
self.tabBarItem.title = @"花时间";
self.navigationItem.title = @"花时间";
}
return self;
}
定义TabBarController
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBarController;
@end
2.AppDelegate.m
#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface AppDelegate ()<UITabBarControllerDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];//实例化window
self.window.backgroundColor = [UIColor whiteColor];//设置window背景
_tabBarController = [[UITabBarController alloc]init]; //实例化选项卡控制器
_tabBarController.delegate = self; //将AppDelegate设置成选项卡控制器的代理???
//实例化3个控制器
FirstViewController *firstViewController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *secondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
ThirdViewController *thirdViewController = [[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil];
//实例化3个导航器,将每个控制器的对象都放到导航器的跟控制器的方法中
UINavigationController *navFirst = [[UINavigationController alloc]initWithRootViewController:firstViewController];
navFirst.title = @"花时间";
UINavigationController *navSecond = [[UINavigationController alloc]initWithRootViewController:secondViewController];
navSecond.title = @"花会友";
UINavigationController *navThird = [[UINavigationController alloc]initWithRootViewController:thirdViewController];
navThird.title = @"我";
//将导航器对象都放到选项卡的子控制器(viewcontroller)中
_tabBarController.viewControllers = [NSArray arrayWithObjects:navFirst,navSecond,navThird, nil];
//设置UIWindow的rootViewController为UITabBarController
_window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
3.每个UIViewController都设置一下TabBarItem的标题
FirstViewController.m
#import "FirstViewController.h"
#import "AppDelegate.h"
@interface FirstViewController ()<UITableViewDataSource,UITableViewDelegate>{
NSMutableArray *_allFlowers;
}
@end
@implementation FirstViewController
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.image = [UIImage imageNamed:@"first.png"];
self.tabBarItem.title = @"花时间";
self.navigationItem.title = @"花时间";
}
return self;
}
相关文章推荐
- block块的用法
- Issues in persisting dynamic entity and view objects using MDS
- IOS常用正则表达式
- Android Drawable 那些不为人知的高效用法
- Android BitmapShader 实战 实现圆形、圆角图片
- Android ListView的上拉刷新,下拉加载功能(pull - to -refesh)
- Android Xfermode 实战 实现圆形、圆角图片
- 发布应用到App Store
- Android中加入百度地图SDK
- iOS开发 -- JSON文件转Plist文件方法
- android开发 缩放到指定比例的尺寸
- android 自定义ViewGroup实现可记载并呈现选择的ListView
- Objective-C 编程艺术
- android 自定义ViewGroup实现可记载并呈现选择的ListView
- ios常用资料
- ios中KeyChain用途
- android自定义布局中的平滑移动
- Android solve设备(手机)旋转过程中出现程序中断问题
- android系统下chmod -R命令无效
- WebViewJavascriptBridge-Obj-C和JavaScript互通消息的桥梁