我有這段代碼。用於Textfield的UIPickerView。
我想DESIGH使
當用戶編輯UITextField
,UIPickerView
和donebutton
顯示,
UIPickerView
通過推donebutton
關閉。
關閉UIPickerView由Donebutton
問題是doneButton
不顯示。
所以,Picker不能關閉。
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];
}
我如何可以修復它的任何想法?
參閱此http://stackoverflow.com/questions/1262574/add-uipickerview-a-button-in-action-sheet-how –
其中完成位於按鈕,後面選擇器視圖或其中?? –
是否顯示導航欄? – manujmv