您的位置:首页 > 移动开发

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: