Date Picker
DatePicker没有delegate
和datasource
,继承自UIControl
:
接下来使用UIDatePicker
实现如下效果:
效果的实现是相当简单的,只需在
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
实现如下图所示的效果:
UIPickerView
有UIPickerViewDataSource
和UIPickerViewDelegate
两个协议
在实现时需要实现其中的一些方法:
- (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];
}
即可实现上述效果
实现两列效果:
- (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
- (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];
}
}