iOS Picker View和Date Picker

Date Picker

DatePicker没有delegatedatasource,继承自UIControl:
接下来使用UIDatePicker实现如下效果:

iOS Picker View和Date Picker_第1张图片
Screen Shot 2015-07-27 at 10.53.20.png

效果的实现是相当简单的,只需在 storyboard的场景中拖入一个 Date Picker控件即可!

- (IBAction)buttonPressed:(UIButton *)sender {
    
    NSDate *selected = [self.datePicker date];
    NSString *message = [[NSString alloc] initWithFormat:@"The date and time you selected is: %@", selected ];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Date and Time Selected" message:message delegate:nil cancelButtonTitle:@"That's so true" otherButtonTitles:nil];
    [alert show];
} 

用代码实现UIAlterView来显示所选择的时间

UIPickerView

实现如下图所示的效果:

iOS Picker View和Date Picker_第2张图片
Screen Shot 2015-07-27 at 15.56.45.png

UIPickerViewUIPickerViewDataSourceUIPickerViewDelegate两个协议
在实现时需要实现其中的一些方法:

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [self.characterNames count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return self.characterNames[row];
} 
 

即可实现上述效果

iOS Picker View和Date Picker_第3张图片
Screen Shot 2015-07-27 at 16.56.38.png

实现两列效果:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.fillingArray = @[@"Ham", @"Turkey", @"Peanut Butter", @"Tuna Salad", @"Chicken Salad", @"Roast Beef", @"Vegemite"];
    self.breadTypes = @[@"White", @"Whole Wheat", @"Rye", @"Sourdough", @"Seven Grain"];
}

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

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == kBreadComponent) {
        return self.breadTypes[row];
    }else {
        return self.fillingArray[row];
    }
}   

实现相互依赖的UIPickerView

iOS Picker View和Date Picker_第4张图片
Screen Shot 2015-07-27 at 17.49.15.png
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *plistURL = [bundle URLForResource:@"statedictionary" withExtension:@"plist"];
    self.stateZips = [NSDictionary dictionaryWithContentsOfURL:plistURL];
    NSArray *allstates = [self.stateZips allKeys];
    NSArray *sortedStates = [allstates sortedArrayUsingSelector:@selector(compare:)];
    self.states = sortedStates;
    NSString *selectedState = self.states[0];
    self.zips = self.stateZips[selectedState];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == kStateComponent) {
        return [self.states count];
    } else {
        return [self.zips count];
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == kStateComponent) {
        return self.states[row];
    } else {
        return self.zips[row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == kStateComponent) {
        NSString *selectedStates = self.states[row];
        self.zips = self.stateZips[selectedStates];
        [self.dependentPicker reloadComponent:kZipComponent];
        [self.dependentPicker selectRow:0 inComponent:kZipComponent animated:YES];
    }
}  

你可能感兴趣的:(iOS Picker View和Date Picker)