Iphone [Tab Bar实现多view切换,Picker,DataPicter实现

本文涉及的产品
可视分析地图(DataV-Atlas),3 个项目,100M 存储空间
数据可视化DataV,5个大屏 1个月
简介: 用Tab Bar Controller处理IPhone多个view切换, 而且还附有创建空项目,picker和DataPicker的实现! 具体步骤: 1.创建一个空项目,选择User Interface->View,命名为rootView。

用Tab Bar Controller处理IPhone多个view切换,

而且还附有创建空项目,picker和DataPicker的实现

具体步骤:

1.创建一个空项目,选择User Interface->View,命名为rootView。

2.然后在控件面板中拖一个Tab Bar Controller的控件,可以往里面添加Tab Button,将Tab Bar Controller的File's Owner改成AppDelegate,可以将其每一个Item View的Class属性改成对应的ViewController

3.在Appdelegate.m中设置根view

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    //通过目录导入xib文件中所有的内容
    NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"rootView" owner:self options:nil];
    NSLog(@"%@",arr);
    self.window.rootViewController = self.rootView;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
4.DateViewPicker的实现

dateViewController.h:

#import <UIKit/UIKit.h>

@interface dataViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIDatePicker *datePicker;
- (IBAction)click:(id)sender;
@property (retain, nonatomic) IBOutlet UILabel *lblShow;
@end

dateViewController.m:

#import "dataViewController.h"

@interface dataViewController ()

@end

@implementation dataViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //self.datePicker.date = [NSData date];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeDate) userInfo:nil repeats:YES];
}

-(void)changeDate
{
    self.datePicker.date = [NSDate date];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_datePicker release];
    [_lblShow release];
    [super dealloc];
}
- (IBAction)click:(id)sender {
    NSData *date = self.datePicker.date;
    NSDateFormatter *formater=[[[NSDateFormatter alloc] init] autorelease];
    //设置日期格式
    formater.dateFormat = @"yyyy-MM-dd HH:mm:ss";//HH代表24时制,hh代表12时制
    NSString *str  = [formater stringFromDate:date];
    //设置时区
    formater.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease];
    date = [formater dateFromString:@"2013-08-15 14:03:00"];
    NSLog(@"字符串转化成日期是:%@",date);
    NSLog(@"%@",str);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前时间" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    self.lblShow.text = str;
    
}
@end

PickerView的实现:

viewController.h:

#import <UIKit/UIKit.h>

//注意:在使用pickerView的时候要将控件拖向File's Owner,将DataSourse和Delegate指向File's Owner,并且要实现他的两个协议

@interface SecondViewController :UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>

@property (retain,nonatomic)IBOutletUIPickerView *picker;

- (IBAction)click:(id)sender;

@property(nonatomic,retain)NSArray *array;

@end


viewController.m:

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *arr = [NSArray arrayWithObjects:@"shanghai",@"nanjign",@"tianjin",@"nantong",@"beijing",nil];
    self.array = arr;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_picker release];
    [super dealloc];
}

#pragma mark datasourse
//每个组件有多少行数据
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.array count];
}
//PickerView有几个组件
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

#pragma mark delegate
//每个组件中的每行显示什么数据
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.array objectAtIndex:row];
}



- (IBAction)click:(id)sender {
    int row = [self.picker selectedRowInComponent:0];//获得第几个组件中被选中的行数
    NSString *str = [self.array objectAtIndex:row];//通过第几行,在数据中获得被选中的字符串
    
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"title" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show];
}
@end


效果图:



如果有两个Component的话:

viewController.h:

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
@property (retain, nonatomic) IBOutlet UIPickerView *picker;
@property(nonatomic,retain)NSArray *array;
@property(nonatomic,retain)NSArray *array2;
- (IBAction)click:(id)sender;
@end

viewController.m:

#import "FirstViewController.h"
#define component_0 0
#define other_component 1
@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *array = [NSArray arrayWithObjects:@"上海",@"北京",@"天津",@"成都",@"台湾",@"香港",@"江苏",@"武汉",@"黑龙江",@"浙江", nil];
    self.array = array;
    
    array = @[@"路飞",@"乔巴",@"香吉士",@"索隆",@"娜美",@"罗宾",@"乌索普",@"弗兰奇",@"骷髅头"];
    self.array2 = array;
    //用来设置默认选项
    [self.picker selectRow:2 inComponent:component_0 animated:YES];   
    [self.picker selectRow:3 inComponent:other_component animated:YES];

}

//每个组件有几行数据
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    //创建数据
    if(component == component_0)
    {
        return [self.array count];   //动态获取数字
    }else{
        return [self.array2 count];
    }
}

#pragma mark delegate
//每个组件的每行显示什么数据
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component == component_0)
    {
        return [self.array objectAtIndex:row];
    }else{
        return [self.array2 objectAtIndex:row];
    }
    
}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
//当你对一个pickerView进行了一次操作之后都会被调用
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *str = nil;
    if(component == component_0)
    {
        str = [self.array objectAtIndex:row];
    }
    else
    {
        str = [self.array2 objectAtIndex:row];
    }
    NSLog(@"%@",str);
}

- (void)dealloc {
    [_picker release];
    [_array release];
    [_array2 release];
    [super dealloc];
}
- (IBAction)click:(id)sender {
    int row = [self.picker selectedRowInComponent:component_0];   //第几个组件中被选中的行数
    NSString * str = [self.array objectAtIndex:row];   //通过第几行,在数据中获得被选中的字符串
    row = [self.picker selectedRowInComponent:other_component];
    NSString *str1 = [self.array2 objectAtIndex:row];
    
    NSString *str3 = [NSString stringWithFormat:@"%@ %@",str,str1];
    
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Title" message:str3 delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
@end



相关实践学习
DataV Board用户界面概览
本实验带领用户熟悉DataV Board这款可视化产品的用户界面
阿里云实时数仓实战 - 项目介绍及架构设计
课程简介 1)学习搭建一个数据仓库的过程,理解数据在整个数仓架构的从采集、存储、计算、输出、展示的整个业务流程。 2)整个数仓体系完全搭建在阿里云架构上,理解并学会运用各个服务组件,了解各个组件之间如何配合联动。 3&nbsp;)前置知识要求 &nbsp; 课程大纲 第一章&nbsp;了解数据仓库概念 初步了解数据仓库是干什么的 第二章&nbsp;按照企业开发的标准去搭建一个数据仓库 数据仓库的需求是什么 架构 怎么选型怎么购买服务器 第三章&nbsp;数据生成模块 用户形成数据的一个准备 按照企业的标准,准备了十一张用户行为表 方便使用 第四章&nbsp;采集模块的搭建 购买阿里云服务器 安装 JDK 安装 Flume 第五章&nbsp;用户行为数据仓库 严格按照企业的标准开发 第六章&nbsp;搭建业务数仓理论基础和对表的分类同步 第七章&nbsp;业务数仓的搭建&nbsp; 业务行为数仓效果图&nbsp;&nbsp;
相关文章
|
Android开发
Android项目Tab类型主界面大总结 Fragment+TabPageIndicator+ViewPager
Android项目Tab类型主界面大总结 Fragment+TabPageIndicator+ViewPager
Android项目Tab类型主界面大总结 Fragment+TabPageIndicator+ViewPager