2013-08-30 43 views
0

我有這段代碼。用於Textfield的UIPickerView。
我想DESIGH使
當用戶編輯UITextFieldUIPickerViewdonebutton顯示,
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]; 
} 

我如何可以修復它的任何想法?

+0

參閱此http://stackoverflow.com/questions/1262574/add-uipickerview-a-button-in-action-sheet-how –

+0

其中完成位於按鈕,後面選擇器視圖或其中?? –

+0

是否顯示導航欄? – manujmv

回答

1

您也可以做到這一點通過設置UITextField對象的inputView

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中會是:

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

    return YES; 
} 

其屬性accessoryView_可以用實例好地方去做)

解僱皮克只不過是

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

當然,你必須保持與屬性或伊娃一個參考textField

+0

謝謝你的回答,它完美的工作。 –

0

如果我理解正確的話,你必須顯示導航欄上的「完成」按鈕的問題(右側) 。如果當前的UIViewController不是UINavigationController層次結構的一部分,或者未以UINavigationController作爲rootViewController初始化,導航項目將無法正常工作。

你可以通過下面的代碼片段確認它,如果它工作,那麼視圖控制器是UINavigationController的一部分,如果不是,那麼你應該檢查你的UINavigation層次結構。

self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"done", @"Done") 
              style:UIBarButtonItemStyleDone 
              target:self action:@selector(doneAction)]; 
+0

謝謝你的回答。這非常有用。 –