码迷,mamicode.com
首页 > 其他好文 > 详细

UIDatePicker,UIPickerView,MultiComponentPicker

时间:2015-08-04 00:28:12      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

UIDatePicker

技术分享
属性:
Mode:Date and Time
Locale:
Interval:
最大值,最小值
    NSDate *date=[self.dataPicker date];
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *date2=[formatter stringFromDate:date];
View Code

UIPickerView

技术分享
代理方法:
delegate,dataSource

NSArray *data=[[NSArray alloc]initWithObjects:@"中国",@"日本",@"美国",@"法国",@"英国",@"意大利", nil];

#pragma mark --UIDataPicker DataSource Method--

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;//返回组件的个数,即列数
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [data count];//行数
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [data objectAtIndex:row];//每一行对应的标题
}


    NSInteger row=[singlePick selectedRowInComponent:0];
    NSString *selected=[data objectAtIndex:row];
    NSString *title=[NSString stringWithFormat:@"当前选中的国家是:%@",selected];
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:title message:nil delegate:nil cancelButtonTitle:@"确定"
                                           otherButtonTitles:nil, nil];
    [alertView show];
View Code

多组件选择器:

技术分享
#import "ThridViewController.h"

@interface ThridViewController ()
@property (weak, nonatomic) IBOutlet UIPickerView *picker;

@property (nonatomic,strong) NSDictionary *dic;
@property (nonatomic,strong) NSArray *provinces;
@property (nonatomic,strong) NSArray *city;
@end

@implementation ThridViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSString *plistCity=[[NSBundle mainBundle]pathForResource:@"cities" ofType:@"plist"];
    NSDictionary *dic=[NSDictionary dictionaryWithContentsOfFile:plistCity];
    self.dic=dic;
    NSArray *arrayKey=[dic allKeys];
    self.provinces=[arrayKey sortedArrayUsingSelector:@selector(compare:)];
    NSString *provicesName=[self.provinces objectAtIndex:0];
    self.city=  [dic objectForKey:provicesName];
}

- (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.
}
*/


#pragma mark
          //numberOfComponentsInPickerView
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0) {
        return [self.provinces count];
    }
    else
    {
        return [self.city count];
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component==0) {
        return [self.provinces objectAtIndex:row];
    }
    return [self.city objectAtIndex:row];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (component==0) {
        return 200;
    }
    return 100;
}



#pragma mark --delegate--
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    if (component==0) {
        NSString *keyName=[self.provinces objectAtIndex:row];
        NSArray *array=[self.dic objectForKey:keyName];
        self.city=array;
        [self.picker selectRow:0 inComponent:1 animated:YES];
        [self.picker reloadComponent:1];
   }
}

- (IBAction)btnOK:(id)sender {
       NSInteger row1=   [self.picker selectedRowInComponent:0];
    NSString *proName=[self.provinces objectAtIndex:row1];
    NSInteger row2=[self.picker selectedRowInComponent:1];
    NSString *cityName=[self.city objectAtIndex:row2];
    NSString *title=[NSString stringWithFormat:@"您选择的省份:%@,城市:%@",proName,cityName];
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:title message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   NSString *content= alertView.title;
    NSLog(@"%ld....%@",(long)buttonIndex,content);
}
View Code

 

 

 

UIDatePicker,UIPickerView,MultiComponentPicker

标签:

原文地址:http://www.cnblogs.com/iOS-Code/p/4700617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!