武汉ios培训
达内武汉中心

15271940953

热门课程

UITabBarController多视图控制器

  • 时间:2016-06-02
  • 发布:Developer_Man
  • 来源:C博客

武汉ios培训导师讲到UITabBarController为多视图控制器,可以切换不同视图,今天我们来学习一下其简单用法。

代码实例
ZYViewController.h:

#import <UIKit/UIKit.h>  
#import "ZYFirstViewController.h"  
#import "ZYSecondViewController.h"  
 
@interface ZYViewController : UIViewController  
 
@property(nonatomic,strong) ZYFirstViewController *firstViewController;//第一个视图  
 
@property(nonatomic,strong) ZYSecondViewController *secondViewController;//第二个视图  
 
@property(nonatomic,strong) UITabBarController *taBarController;//多视图控制器  
 
@property (strong,nonatomic) UINavigationController *firstnavigationController;//第一个视图导航栏  
 
@property (strong,nonatomic) UINavigationController *secondnavigationController;//第二个视图导航栏  
 
@end  
ZYViewController.m:

@synthesize firstViewController;  
@synthesize secondViewController;  
@synthesize taBarController;  
@synthesize firstnavigationController;  
@synthesize secondnavigationController;  
 
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    // Do any additional setup after loading the view, typically from a nib.  
    self.title = @"UITabBarControllerTest";  
    self.firstViewController = [[ZYFirstViewController alloc] initWithNibName:nil bundle:nil];  
    firstnavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];  
    self.secondViewController = [[ZYSecondViewController alloc] initWithNibName:nil bundle:nil];  
    secondnavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];  
    NSArray *twoViewControllers = [[NSArray alloc] initWithObjects:firstnavigationController,secondnavigationController, nil];//实例化视图数组  
    self.taBarController = [[UITabBarController alloc] init];  
    [self.taBarController setViewControllers:twoViewControllers];  
    [self.view addSubview:taBarController.view];  
}  

ZYFirstViewController.m:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
{  
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
    if (self) {//由于多视图控制器加载时候不会加载非首页面,所以把初始化放在该方法中  
        self.title = @"First";//设置标题,并会自动赋给多视图控制器按钮  
        self.tabBarItem.image = [UIImage imageNamed:@"first.png"];//添加图片  
        self.view.backgroundColor = [UIColor whiteColor];  
    }  
    return self;  
}  

ZYSecondViewController.m:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
{  
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
    if (self) {  
        // Custom initialization  
        self.title = @"Second";  
        self.tabBarItem.image = [UIImage imageNamed:@"second.png"];  
        self.view.backgroundColor = [UIColor whiteColor];  
 
    }  
    return self;  

上一篇:武汉ios培训:UITextField控件
下一篇:UIBarButtonItem导航栏添加按钮

想学ios,到哪学比较好

学习ios开发的最佳之选是什么?

学ios,有前途吗?

成为ios开发工程师,培训和自学哪条路更适合你?

选择城市和中心
贵州省

广西省

海南省