0

私はこのコードを持っています。Textfield の UIPickerView。 ユーザー編集時、とが表示されている場合は、 を押して閉じる
ようにしたいと思います。
UITextFieldUIPickerViewdonebutton
UIPickerViewdonebutton

問題は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];
}

どうすれば修正できるかについて何か考えはありますか?

4

2 に答える 2

2

オブジェクトのinputViewを設定することでそれを行うこともできます。UITextField

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

    return YES;
}

ここで、プロパティ accessoriesView_ は次のようにインスタンス化できます。

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 はそれを行うのに適した場所です)

却下ピッカーは単純です

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

もちろんtextField、プロパティまたはivarを使用してへの参照を保持する必要があります

于 2013-08-30T10:19:41.410 に答える