{"id":573,"date":"2018-02-23T23:40:08","date_gmt":"2018-02-23T15:40:08","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/ios%e9%80%89%e6%8b%a9%e5%99%a8%ef%bc%88pickers%ef%bc%89\/"},"modified":"2018-02-23T23:40:08","modified_gmt":"2018-02-23T15:40:08","slug":"ios%e9%80%89%e6%8b%a9%e5%99%a8%ef%bc%88pickers%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=573","title":{"rendered":"iOS\u9009\u62e9\u5668\uff08Pickers\uff09"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h2>\u9009\u62e9\u5668\u7684\u4f7f\u7528<\/h2>\n<p>\u9009\u62e9\u5668\u662f\u4e00\u4e2a\u53ef\u6eda\u52a8\u89c6\u56fe\uff0c\u7528\u4e8e\u9009\u53d6\u5217\u8868\u9879\u4e2d\u7684\u503c\u3002<\/p>\n<h3>\u91cd\u8981\u7684\u5c5e\u6027<\/h3>\n<ul>\n<li>\ndelegate\n<\/li>\n<li>\ndataSource\n<\/li>\n<\/ul>\n<h3>\u91cd\u8981\u7684\u65b9\u6cd5<\/h3>\n<pre>\n- (void)reloadAllComponents\n<\/pre>\n<pre>\n- (void)reloadComponent:(NSInteger)component\n<\/pre>\n<pre>\n- (NSInteger)selectedRowInComponent:(NSInteger)component\n<\/pre>\n<pre>\n- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component\n\u00a0 animated:(BOOL)animated\n<\/pre>\n<h3>\u4fee\u6539 ViewController.h<\/h3>\n<p>\u6211\u4eec\u5c06\u6dfb\u52a0\u4e00\u4e2a\u6587\u672c\u5b57\u6bb5\u3001\u9009\u62e9\u5668\u89c6\u56fe\u548c\u4e00\u4e2a\u6570\u7ec4\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u91c7\u7528UITextFieldDelegate\u3001UIPickerViewDataSource\u3001UIPickerViewDelegate\u7684\u534f\u8bae\u3002ViewController.h\u6587\u4ef6\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>\n#import &lt;UIKit\/UIKit.h&gt;\n\n@interface ViewController : UIViewController\n&lt;UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate&gt;\n{\n    \n    UITextField *myTextField;\n    UIPickerView *myPickerView;\n    NSArray *pickerArray;\n}\n@end\n<\/pre>\n<h3>\u6dfb\u52a0\u81ea\u5b9a\u4e49\u65b9\u6cd5 addPickerView<\/h3>\n<pre>\n-(void)addPickerView{\n    pickerArray = [[NSArray alloc]initWithObjects:@\"Chess\",\n    @\"Cricket\",@\"Football\",@\"Tennis\",@\"Volleyball\", nil];\n    myTextField = [[UITextField alloc]initWithFrame:\n    CGRectMake(10, 100, 300, 30)];\n    myTextField.borderStyle = UITextBorderStyleRoundedRect;\n    myTextField.textAlignment = UITextAlignmentCenter;\n    myTextField.delegate = self;\n    [self.view addSubview:myTextField];\n    [myTextField setPlaceholder:@\"Pick a Sport\"];\n    myPickerView = [[UIPickerView alloc]init];\n    myPickerView.dataSource = self;\n    myPickerView.delegate = self;\n    myPickerView.showsSelectionIndicator = YES;\n    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] \n    initWithTitle:@\"Done\" style:UIBarButtonItemStyleDone \n    target:self action:@selector(done:)];\n    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:\n    CGRectMake(0, self.view.frame.size.height-\n    myPickerView.frame.size.height-50, 320, 50)];\n    [toolBar setBarStyle:UIBarStyleBlackOpaque];\n    NSArray *toolbarItems = [NSArray arrayWithObjects: \n    doneButton, nil];\n    [toolBar setItems:toolbarItems];\n    myTextField.inputView = myPickerView;\n    myTextField.inputAccessoryView = toolBar;\n\n}\n<\/pre>\n<h3>\u6267\u884c\u59d4\u6258\uff0c\u5982\u4e0b\u6240\u793a:<\/h3>\n<pre>\n#pragma mark - Text field delegates\n\n-(void)textFieldDidBeginEditing:(UITextField *)textField{\n    if ([textField.text isEqualToString:@\"\"]) {\n        [self dateChanged:nil];\n    }\n}\n#pragma mark - Picker View Data source\n-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{\n    return 1;\n}\n-(NSInteger)pickerView:(UIPickerView *)pickerView \n numberOfRowsInComponent:(NSInteger)component{\n    return [pickerArray count];\n}\n\n#pragma mark- Picker View Delegate\n\n-(void)pickerView:(UIPickerView *)pickerView didSelectRow:\n (NSInteger)row inComponent:(NSInteger)component{\n    [myTextField setText:[pickerArray objectAtIndex:row]];\n}\n- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:\n  (NSInteger)row forComponent:(NSInteger)component{\n    return [pickerArray objectAtIndex:row];\n}\n<\/pre>\n<p>\u5728ViewController.m\u4fee\u6539viewDidLoad\uff0c\u5982\u4e0b\u6240\u793a:<\/p>\n<pre>\n(void)viewDidLoad\n{\n   [super viewDidLoad];\n   [self addPickerView];\n}\n<\/pre>\n<h3>\u8f93\u51fa<\/h3>\n<p>\u73b0\u5728\u5f53\u6211\u4eec\u8fd0\u884c\u8be5\u5e94\u7528\u7a0b\u5e8f\u6211\u4eec\u4f1a\u770b\u5230\u4e0b\u9762\u7684\u8f93\u51fa:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"pickerOutput1\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/pickerOutput1.jpg\" width=\"250\" height=\"489\" data-pinit=\"registered\"><\/p>\n<p>\u6587\u672c\u9009\u62e9\u5668\u89c6\u56fe\u5982\u4e0b\u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u53d6\u6211\u4eec\u9700\u8981\u7684\u503c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"pickerOutput2\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/pickerOutput2.jpg\" width=\"250\" height=\"497\" data-pinit=\"registered\"><\/p>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">\u9009\u62e9\u5668\u7684\u4f7f\u7528 \u9009\u62e9\u5668\u662f\u4e00\u4e2a\u53ef\u6eda\u52a8\u89c6\u56fe\uff0c\u7528\u4e8e\u9009\u53d6\u5217\u8868\u9879\u4e2d\u7684\u503c\u3002 \u91cd\u8981\u7684\u5c5e\u6027 delegate&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=573\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,26],"tags":[74,93],"class_list":["post-573","post","type-post","status-publish","format-standard","hentry","category-ios","category-mobile","tag-ios","tag-mobile","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=573"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/573\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}