UIPickerView 簡單使用

//實現一個計劃設置時間的選擇
//上個界面是時間列表的其中一個cell點擊進入此頁面
//.h文件
#import "MMViewController.h"

@protocol ScheuduleStepTimeSettingDelegate <NSObject>

- (void)stepTimeSettingWithIndexRow:(NSInteger)indexRow hour:(NSString *)hour minute:(NSString *)minute;

@end

@interface ScheduleStepTimeSettingVC : MMViewController

@property (nonatomic, weak) id<ScheuduleStepTimeSettingDelegate> delegate;

@property (nonatomic, assign) NSInteger currentIndexRow;

@end
//.m文件
#import "ScheduleStepTimeSettingVC.h"

@interface ScheduleStepTimeSettingVC () <UIPickerViewDataSource, UIPickerViewDelegate>

@end

@implementation ScheduleStepTimeSettingVC{
    UIPickerView *timePickView;
    
    NSMutableArray *hourDataArr;
    NSMutableArray *minuteDataArr;
    
    NSString *currentHour;
    NSString *currentMinute;
}

- (void)rightBtnDidClick{    //完成按鈕
    if ([self.delegate respondsToSelector:@selector(stepTimeSettingWithIndexRow:hour:minute:)]) {
        [self.delegate stepTimeSettingWithIndexRow:self.currentIndexRow hour:currentHour minute:currentMinute];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationLabel.text = @"選擇時間";
    [self.rightBtn setTitle:@"完成" forState:UIControlStateNormal];

    [self initArr];
    
    timePickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0,DEVICE_W, 320)];
    timePickView.delegate = self;
    timePickView.dataSource = self;
    [self.view addSubview:timePickView];
}
- (void)initArr{
    hourDataArr = [[NSMutableArray alloc] init];
    for (int i = 0 ; i < 24; i ++) {
        NSString *str;
        if (i < 10) {
            str = [@"0" stringByAppendingString:[NSString stringWithFormat:@"%d",i]];
        }else{
            str = [NSString stringWithFormat:@"%d",i];
        }
        [hourDataArr addObject:str];
    }
    
    minuteDataArr = [[NSMutableArray alloc] init];
    for (int i = 0 ; i < 60; i ++) {
        NSString *str;
        if (i < 10) {
            str = [@"0" stringByAppendingString:[NSString stringWithFormat:@"%d",i]];
        }else{
            str = [NSString stringWithFormat:@"%d",i];
        }
        [minuteDataArr addObject:str];
    }
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return hourDataArr.count;
    }else if(component == 1){
        return minuteDataArr.count;
    }else{
        return 0;
    }
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return  [hourDataArr objectAtIndex:row];;
    }else if(component == 1){
        return [minuteDataArr objectAtIndex:row];
    }else{
        return nil;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        currentHour = [hourDataArr objectAtIndex:row];;
    }else if(component == 1){
        currentMinute = [minuteDataArr objectAtIndex:row];
    }
}
@end
相關文章
相關標籤/搜索