[objective-c]关闭由 Donebutton UIPickerView

标签: ios Objective-C
发布时间: 2016/10/4 1:40:09
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有此代码。UIPickerView 为文本字段的。
我想要设计这样
当用户编辑 UITextFieldUIPickerViewdonebutton 的显示
UIPickerView按下关闭 donebutton

问题是 doneButton 则不会显示。
所以,选择器不能关闭。

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField *textField1;
@property (weak, nonatomic) IBOutlet UITextField *textField2;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    UIPickerView *picker1;
    NSString *pic1_str;
}
@synthesize textField1;
@synthesize textField2;

- (void)viewDidLoad
{
    [super viewDidLoad];

    textField1.delegate = self;

    picker1 = [[UIPickerView alloc] init];
    picker1.frame = CGRectMake(0, 460, 320, 216);
    picker1.showsSelectionIndicator = YES;
    picker1.delegate = self;
    picker1.dataSource = self;
    picker1.tag = 1;
    [self.view addSubview:picker1];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        [self showPicker1];
        return NO;
}

- (void)showPicker1 {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationDelegate:self];
    picker1.frame = CGRectMake(0, 204, 320, 216);
    [UIView commitAnimations];

    if (!self.navigationItem.rightBarButtonItem) {
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
        [self.navigationItem setRightBarButtonItem:doneButton animated:YES];
    }
}

- (void)done:(id)sender {
    [self hidePicker];
    [self.navigationItem setRightBarButtonItem:nil animated:YES];
}

- (void)hidePicker {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationDelegate:self];
    picker1.frame = CGRectMake(0, 420, 320, 216);
    [UIView commitAnimations];
}

如何可以修复它的是什么意思?

解决方法 1:

你也可以通过设置 inputViewUITextField 对象︰

- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
    textField.inputView = _pickerView;
    textField.inputAccessoryView = self.accessoryView_;

    return YES;
}

在哪里可以使用实例化属性 accessoryView_:

self.accessoryView_ = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, _pickerView.frame.size.width, 40)];

        [(UIToolbar *) self.accessoryView_ setItems:@[
                                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissPicker)]
         ]];

在您的代码的某个地方 (viewDidLoad 会做它的好地方)

dismissPicker 只是

- (void) dismissPicker
{
    [_textField resignFirstResponder];
}

当然,你必须保持对引用 textField 与属性或埃法尔

官方微信
官方QQ群
31647020