iOS UIPickerView 选择时间点

2023-03-30  本文已影响0人  炸街程序猿

1.创建控件

UIPickerView *boottime;
UIPickerView *shutdown;
int currenthour;
int currentminute;
NSArray *hour;
NSArray *minute;
NSString *boothour;
NSString *bootmintue;
NSString *str1;
NSString *str2;
  1. 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];
}
image.png
上一篇下一篇

猜你喜欢

热点阅读