2013-08-16 73 views
1

我研究了這一點,但只找到瞭如何切換鍵盤的鍵盤。現在我有一個UIPicker,當點擊一個文本框時會出現這個UIPicker。在Picker中它們有三個選項,第三個是自定義。我想要發生的是當選擇自定義時,UIPicker被鍵盤取代。我猜這將用if (select == 2) { }方法完成。我不需要一大堆代碼,只需要替換Picker的方法。另外,我想用後退按鈕製作工具欄,以便返回到選擇器。我現在有一個帶有doneButton的工具欄來釋放拾取器,但是當出現鍵盤並且按鈕能夠將鍵盤切換回拾取器時,我需要使按鈕出現。切換鍵盤的UIPicker

到目前爲止,我的選擇器,toolabr和文本字段有:

... 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    NSArray *array = [[NSArray alloc] initWithObjects:@"...",@"...",@"Custom", nil]; 
    self.PickerData = array; 

... 

    UIToolbar *toolBar = [[UIToolbar alloc] init]; 
    toolBar.barStyle = UIBarStyleBlackOpaque; 
    [toolBar sizeToFit]; 

... 

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                       target:self 
                       action:nil]; 
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                       target:self 
                       action:@selector(releasePicker)]; 


    Picker = [[UIPickerView alloc] init]; 
    Picker.showsSelectionIndicator = YES; 
    Picker.delegate = self; 
    doneButton.image = [UIImage imageNamed:@"button.png"]; 

    [toolBar setItems:@[flexSpace, doneButton] animated:YES]; 
    self.habitField.inputAccessoryView = toolBar; 

    [self.habitField setEnabled: YES]; 

    [self.habitField setInputView:Picker]; 

} 
- (void)releasePicker { 
    [self.habitField resignFirstResponder]; 
} 

... 

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 
    return 1; 
} 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    return [PickerData count]; 
} 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    return [self.PickerData objectAtIndex:row]; 
} 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    int select = row; 
    if (select == 0) { 

     ... 
    } 
    if (select == 1) { 

     ... 
    } 
    if (select == 2) { 

     ... 

    } 
} 


@end 

回答

1
if (select == 2) { 
     [self.habitField resignFirstResponder]; 
     [self.habitField setInputView:nil]; 
     [self.habitField becomeFirstResponder]; 
} 

這基本上是隱藏UIPickerView然後刪除它,然後彈出鍵盤。

如果你想UIPicker振作起來使用此代碼:

[self.habitField resignFirstResponder]; 
[self.habitField setInputView:PickerData]; 
[self.habitField becomeFirstResponder]; 
+0

我怎樣才能使按鈕把選擇器回來? –

+0

@ user2660874請參閱我的編輯中的代碼 –