//DatePicker
- (IBAction)buttonPressed {
NSDate *selected = [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:nilcancelButtonTitle:@"Yes, I did."otherButtonTitles:nil];
[alert show];}
//single component
@interface BIDSingleComponentPickerViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
@property (strong, nonatomic) IBOutlet UIPickerView *singlePicker;@property (strong, nonatomic) NSArray *pickerData;
- (IBAction)buttonPressed;
@end //先声明协议和outlet
- (IBAction)buttonPressed {
NSInteger row = [singlePicker selectedRowInComponent:0];NSString *selected = [pickerData objectAtIndex:row];NSString *title = [[NSString alloc] initWithFormat:
@"You selected %@!", selected];
UIAlertView *alert =
[alert show];} //显示数据
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional
NSArray *array = [[NSArray alloc] initWithObjects:@"Luke", @"Leia",
@"Han", @"Chewbacca", @"Artoo", @"Threepio", @"Lando", nil];self.pickerData = array;
} //声明数据
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerViewnumberOfRowsInComponent:(NSInteger)component {
return [pickerData count];}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {return [pickerData objectAtIndex:row];
} //覆写 协议的方法
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;}
- (NSInteger)pickerView:(UIPickerView *)pickerViewnumberOfRowsInComponent:(NSInteger)component {
if (component == kBreadComponent)return [self.breadTypes count];
return [self.fillingTypes count];}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)rowforComponent:(NSInteger)component {
if (component == kBreadComponent)
return [self.breadTypes objectAtIndex:row];
return [self.fillingTypes objectAtIndex:row];}
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSBundle *bundle = [NSBundle mainBundle];
NSURL *plistURL = [bundle URLForResource:@"statedictionary"
withExtension:@"plist"];NSDictionary *dictionary = [NSDictionary
dictionaryWithContentsOfURL:plistURL];
self.stateZips = dictionary;
NSArray *components = [self.stateZips allKeys];
NSArray *sorted = [components sortedArrayUsingSelector:
@selector(compare:)];self.states = sorted;
delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];
NSString *selectedState = [self.states objectAtIndex:0];NSArray *array = [stateZips objectForKey:selectedState];self.zips = array;
} //处理数据
#pragma mark -
#pragma mark Picker Data Source Methods
-
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;}
-
- (NSInteger)pickerView:(UIPickerView *)pickerViewnumberOfRowsInComponent:(NSInteger)component {
if (component == kStateComponent)return [self.states count];
return [self.zips count];}
#pragma mark Picker Delegate Methods
-
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)rowforComponent:(NSInteger)component {
if (component == kStateComponent)
return [self.states objectAtIndex:row];return [self.zips objectAtIndex:row];}
-
- (void)pickerView:(UIPickerView *)pickerViewdidSelectRow:(NSInteger)row
inComponent:(NSInteger)component {if (component == kStateComponent) {
NSString *selectedState = [self.states objectAtIndex:row];NSArray *array = [stateZips objectForKey:selectedState];self.zips = array;
[picker selectRow:0 inComponent:kZipComponent animated:YES];[picker reloadComponent:kZipComponent];}}
-