TabBar实现页面跳转(AppDelegate +NavigationViewController + TabBarViewController)

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;

}

 

原文地址:https://www.cnblogs.com/yuyu-2012/p/4739033.html