iPhone第四节:UIDatePicker、UIPickerView

来源:互联网 发布:测试常用的linux命令 编辑:程序博客网 时间:2024/05/01 04:57

UIDatePicker、UIPickerView


#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property (nonatomic, strong) UILabel * showLabel;@end#import "ViewController.h"#import "PickViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize showLabel;- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];        self.view.backgroundColor = [UIColor whiteColor];        //给出frame,就可以显示出来(高216)(周几,月份,日期,小时,分钟,上午下午)    UIDatePicker * datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 20, 300, 216)];    datePicker.datePickerMode = UIDatePickerModeTime; //只有时间(小时分钟,上下午)    datePicker.datePickerMode = UIDatePickerModeDate; //只有日期(月,日,年)    datePicker.datePickerMode = UIDatePickerModeCountDownTimer; //倒数计时    datePicker.datePickerMode = UIDatePickerModeDateAndTime; //默认    //不同的时间间隔显示分钟,要能够让60整除    datePicker.minuteInterval = 5;    //日期范围    datePicker.minimumDate = [self dateFromString:@"2000-01-01 00:00:00"];    datePicker.maximumDate = [NSDate date];    //设置默认显示的日期    datePicker.date = [NSDate date];    [datePicker setDate:[self dateFromString:@"2000-01-01 00:00:00"] animated:YES];    //显示    [self.view addSubview:datePicker];    //挂接委托    [datePicker addTarget:self action:@selector(show:) forControlEvents:UIControlEventValueChanged];        //用来显示日期    showLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 300, 200, 30)];    showLabel.font = [UIFont systemFontOfSize:16.0];    showLabel.textColor = [UIColor blackColor];    showLabel.textAlignment = NSTextAlignmentCenter;    showLabel.backgroundColor = [UIColor clearColor];    [self.view addSubview:showLabel];    #pragma mark ==Button    UIButton * btnLogin21 = [UIButton buttonWithType:UIButtonTypeCustom];    btnLogin21.frame = CGRectMake(45, 415, 230, 45);    btnLogin21.showsTouchWhenHighlighted = YES;    [btnLogin21 setTitle:@"PickerView" forState:UIControlStateNormal];    [btnLogin21 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [btnLogin21 addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btnLogin21];}//字符串转日期- (NSDate *)dateFromString:(NSString *)dateString{    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    return [dateFormatter dateFromString:dateString];}//日期转字符串- (NSString *)stringFromDate:(NSDate *)date{    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    return [dateFormatter stringFromDate:date];}//显示日期- (void)show:(UIDatePicker *)sender{    showLabel.text = [self stringFromDate:sender.date];}- (void)test{    PickViewController * pickerView = [[PickViewController alloc] init];    pickerView.modalTransitionStyle = 1;    [self presentViewController:pickerView animated:YES completion:nil];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

#import <UIKit/UIKit.h>@interface PickViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>@property (nonatomic, strong) UIPickerView * pickerView;@property (nonatomic, strong) NSMutableArray * countryArray;@property (nonatomic, strong) NSMutableArray * provinceArray;@property (nonatomic, strong) NSMutableArray * cityArray;@property (nonatomic, strong) NSMutableDictionary * countryProvinceDic;@property (nonatomic, strong) NSMutableDictionary * provinceCityDic;@property (nonatomic, strong) NSString * selectedStr;@end#import "PickViewController.h"@interface PickViewController (){    NSString * str1, * str2, * str3;}@end@implementation PickViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    [self initPickerView];    //国家数组    _countryArray = [NSMutableArray arrayWithObjects:@"中国", @"美国", @"英国", @"日本", nil];    //各省数组    NSMutableArray * chinaProvinceArray = [NSMutableArray arrayWithObjects:@"北京", @"河南", @"河北", @"湖南", nil];    NSMutableArray * americaProvinceArray = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil];    NSMutableArray * englandProvinceArray = [NSMutableArray arrayWithObjects:@"A", @"B", nil];    NSMutableArray * japanProvinceArray = [NSMutableArray arrayWithObjects:@"aa", nil];    //各市数组    NSMutableArray * beijingCityArray = [NSMutableArray arrayWithObjects:@"北京1", @"北京2", @"北京3", nil];    NSMutableArray * henanCityArray = [NSMutableArray arrayWithObjects:@"河南1", @"河南2", @"河南3", nil];    NSMutableArray * hebeiCityArray = [NSMutableArray arrayWithObjects:@"河北1", @"河北2", @"河北3", nil];    NSMutableArray * hunanCityArray = [NSMutableArray arrayWithObjects:@"湖南1", @"湖南2", @"湖南3", nil];        NSMutableArray * City1Array = [NSMutableArray arrayWithObjects:@"1111", @"1122",  nil];    NSMutableArray * City2Array = [NSMutableArray arrayWithObjects:@"2211", @"2222",  nil];    NSMutableArray * City3Array = [NSMutableArray arrayWithObjects:@"3311", @"3322",  nil];        NSMutableArray * ACityArray = [NSMutableArray arrayWithObjects:@"AAA", @"ABB", nil];    NSMutableArray * BCityArray = [NSMutableArray arrayWithObjects:@"BAA", @"BBB", nil];        NSMutableArray * aaCityArray = [NSMutableArray arrayWithObjects:@"aa", nil];    //各市数组放到市数组    _cityArray = [NSMutableArray arrayWithObjects:beijingCityArray, henanCityArray, hebeiCityArray, hunanCityArray, City1Array, City2Array, City3Array, ACityArray, BCityArray, aaCityArray, nil];    NSMutableArray * provinceArray = [NSMutableArray arrayWithObjects:@"北京", @"河南", @"河北", @"湖南", @"1", @"2", @"3", @"A", @"B", @"aa", nil];    _provinceCityDic = [NSMutableDictionary dictionaryWithObjects:_cityArray forKeys:provinceArray];//    NSMutableArray * cityArrayChina = [NSMutableArray arrayWithObjects:beijingCityArray, henanCityArray, hebeiCityArray, hunanCityArray, nil];//    NSMutableArray * cityArrayAmerica = [NSMutableArray arrayWithObjects:City1Array, City2Array, City3Array, nil];//    NSMutableArray * cityArrayEngland = [NSMutableArray arrayWithObjects:ACityArray, BCityArray, nil];//    NSMutableArray * cityArrayJapan = [NSMutableArray arrayWithObjects:aaCityArray, nil];//    NSMutableDictionary * dic1 = [NSMutableDictionary dictionaryWithObjects:cityArrayChina forKeys:chinaProvinceArray];//    NSMutableDictionary * dic2 = [NSMutableDictionary dictionaryWithObjects:cityArrayAmerica forKeys:americaProvinceArray];//    NSMutableDictionary * dic3 = [NSMutableDictionary dictionaryWithObjects:cityArrayEngland forKeys:englandProvinceArray];//    NSMutableDictionary * dic4 = [NSMutableDictionary dictionaryWithObjects:cityArrayJapan forKeys:japanProvinceArray];//    //各省数组放到省数组//    _provinceArray = [NSMutableArray arrayWithObjects:dic1, dic2, dic3, dic4, nil];    NSMutableArray * provinceArray2 = [NSMutableArray arrayWithObjects:chinaProvinceArray, americaProvinceArray, englandProvinceArray, japanProvinceArray, nil];    //以国家数组的元素为键,省数组的元素为值,放入字典    _countryProvinceDic = [NSMutableDictionary dictionaryWithObjects:provinceArray2 forKeys:_countryArray];    //设置默认显示的省数组    _provinceArray = chinaProvinceArray;    _cityArray = beijingCityArray;}- (void)initPickerView{    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];    _pickerView.backgroundColor = [UIColor whiteColor];    _pickerView.showsSelectionIndicator = YES;    _pickerView.dataSource = self;    _pickerView.delegate = self;    [self.view addSubview:_pickerView];}//返回列数- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return 3;}//某列行数- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    NSInteger row = 0;//    if (component == 0)//    {//        row = [_countryArray count];//    }//    else if (component == 1)//    {//        row = [_provinceArray count];//    }    switch (component)    {        case 0:  //第一列            row = [_countryArray count];            break;        case 1:  //第二列            row = [_provinceArray count];            break;        case 2:            row = [_cityArray count];            break;    }    return row;}//某列某行标题- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{    if (component == 0)    {        return [_countryArray objectAtIndex:row];    }    else if (component == 1)    {        return [_provinceArray objectAtIndex:row];    }    else        return [_cityArray objectAtIndex:row];}//选中某列某行- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{    switch (component)    {        case 0:        {            str1 = [_countryArray objectAtIndex:row];            NSLog(@"%d", row);            _provinceArray = [_countryProvinceDic objectForKeyedSubscript:str1];            str2 = [_provinceArray objectAtIndex:row];            _cityArray = [_provinceCityDic objectForKeyedSubscript:str2];            [_pickerView reloadComponent:1];            [_pickerView reloadComponent:2];            [_pickerView selectRow:0 inComponent:1 animated:YES];            [_pickerView selectRow:0 inComponent:2 animated:YES];            break;        }        case 1:        {            str3 = [_provinceArray objectAtIndex:row];            _cityArray = [_provinceCityDic objectForKeyedSubscript:str3];            [_pickerView reloadComponent:2];            [_pickerView selectRow:0 inComponent:2 animated:YES];            break;        }        case 2:        {            break;        }    }}//列宽- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{    if (component == 0)    {        return 100.0;    }    else if (component == 1)    {        return 120.0;    }    else        return 100.0;}//某列行高- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{    return 40.0;}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end


0 0
原创粉丝点击