我有这个代码。用于文本字段的 UIPickerView。
我想设计,以便
当用户编辑UITextField
和显示时,UIPickerView
通过push 关闭。donebutton
UIPickerView
donebutton
问题是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];
}
关于如何修复它的任何想法?