0

我有这个代码。用于文本字段的 UIPickerView。
我想设计,以便
当用户编辑UITextField和显示时,UIPickerView通过push 关闭。donebutton
UIPickerViewdonebutton

问题是doneButton不显示。
因此,Picker 无法关闭。

视图控制器.h

#import <UIKit/UIKit.h>

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

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

@end

视图控制器.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;
}

可以用以下方式实例化属性 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 将是一个很好的地方)

解雇选择器很简单

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

当然,您必须保留对textField带有属性或 ivar的引用

于 2013-08-30T10:19:41.410 回答
0

如果我理解正确,您将无法在导航栏(右侧)上显示“完成”按钮。UIViewController如果当前不是UINavigationController层次结构的一部分或未初始化UINavigationController为 rootViewController ,则导航项将无法正常工作。

您可以通过下面的代码片段确认这一点,如果它有效,则视图控制器是 的一部分UINavigationController,如果不是,那么您应该检查您的 UINavigation 层次结构。

self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"done", @"Done")
                                             style:UIBarButtonItemStyleDone
                                            target:self action:@selector(doneAction)];
于 2013-08-30T10:36:05.743 回答