UIPickerView 双排选择时间点

1.创建控件

        UIPickerView *boottime;

UIPickerView *shutdown;

int currenthour;

int currentminute;

NSArray *hour;

NSArray *minute;

NSString *boothour;

NSString *bootmintue;

NSString *str1;

NSString *str2;

2. viewDidLoad

hour = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23"];

minute =@[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50",@"51",@"52",@"53",@"54",@"55",@"56",@"58",@"59"];

currenthour =0;

currentminute =0;

3.代理方法 UIPickerViewDataSource,UIPickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

return2;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

NSInteger result;

if(component ==0){

result = hour.count;

}

if(component ==1){

result = minute.count;

}

return result;

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

NSString *result;

if(component ==0){

result = [hourobjectAtIndex:row];

}

if(component ==1){

result = [minuteobjectAtIndex:row];

}

return result;

}

3.

-(void)selectTime{

if(boottime ==nil){

boottime = [[UIPickerViewalloc]init];

boottime.frame =CGRectMake(WIDTH/6,HEIGHT/12,WIDTH/3*2,HEIGHT/6);

boottime.dataSource =self;

boottime.delegate =self;

}

UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"开机时间:"message:@"\n\n\n\n\n\n\n\n\n"preferredStyle:UIAlertControllerStyleActionSheet];

[alertController.viewaddSubview:boottime];

UIAlertAction *actionSelect = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *action) {

currenthour = (int)[boottimeselectedRowInComponent:0];

currentminute = (int)[boottimeselectedRowInComponent:0];

boothour = [hourobjectAtIndex:currenthour];

bootmintue = [minuteobjectAtIndex:currentminute];

str1 = [NSStringstringWithFormat:@"%@:%@",boothour,bootmintue];

[_btnstartsetTitle:str1forState:UIControlStateNormal];

NSLog(@"%@",str1);


}];

[alertController addAction:actionSelect];

UIAlertAction *actionCancel = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

[alertController addAction:actionCancel];

[selfpresentViewController:alertController animated:YEScompletion:nil];

}

-(void)selectShutdown{

if(shutdown ==nil){

shutdown = [[UIPickerViewalloc]init];

shutdown.frame =CGRectMake(WIDTH/6,HEIGHT/12,WIDTH/3*2,HEIGHT/6);

shutdown.dataSource =self;

shutdown.delegate =self;

}

UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"关机时间:"message:@"\n\n\n\n\n\n\n\n\n"preferredStyle:UIAlertControllerStyleActionSheet];

[alertController.viewaddSubview:shutdown];

UIAlertAction *actionSelect = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *action) {

currenthour = (int)[shutdownselectedRowInComponent:0];

currentminute = (int)[shutdownselectedRowInComponent:0];

boothour = [hourobjectAtIndex:currenthour];

bootmintue = [minuteobjectAtIndex:currentminute];

str2 = [NSStringstringWithFormat:@"%@:%@",boothour,bootmintue];

[_btnShutdownsetTitle:str2forState:UIControlStateNormal];

NSLog(@"%@",str2);

}];

[alertController addAction:actionSelect];

UIAlertAction *actionCancel = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

[alertController addAction:actionCancel];

[selfpresentViewController:alertControlleranimated:YEScompletion:nil];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值