2011-06-28 34 views
4

我只是試圖解決UIPickerView - 導航欄上的按鈕或選擇器視圖上方工具欄上的「完成」按鈕。我已經實現了這兩個按鈕,並且我正在嘗試解除選取器視圖並退出第一響應者。UIToolBar上的Done按鈕解除UIPickerView

如何使用工具欄上的「完成」按鈕來解除UIPickerView

這是我的UIToolBar代碼:

UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init]; 
keyboardDoneButtonView.barStyle = UIBarStyleBlack; 
keyboardDoneButtonView.translucent = YES; 
keyboardDoneButtonView.tintColor = nil; 
[keyboardDoneButtonView sizeToFit]; 
UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done" 
                   style:UIBarButtonItemStyleBordered target:self 
                   action:@selector(pickerDoneClicked:)] autorelease]; 

[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]]; 

textField.inputAccessoryView = keyboardDoneButtonView; 

有人能幫助我嗎?

+1

沒有嘗試過這個自己:到底什麼是你的問題/問題? pickerView不會消失嗎?如果這不起作用,你可以手動向下動畫並調用removeFromSuperview,我猜。那有意義嗎? –

+0

如果可能的話,我會嘗試並複製它。如果我可以得到一個工作代碼,我會分享我與你有什麼 – justin

+0

@slev:謝謝你。 – Legolas

回答

22

雖然我確定我的測試應用程序比較簡單得多,但希望結構仍適用於您的應用程序。

實質上,這就是我所做的一切。我在IB中設置了UIPickerView,UIDatePickerViewUITextField。 pickerView的dataSourcedelegate都鏈接到文件的所有者,就像textField的delegate一樣。

在我的頭,我把它們都具有以下結構

UISomething *object; 
@property (nonatomic, retain) IBOutlet UISomething *object; 

我也得到了鏈接的協議(<UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate>)聲明。在實現文件中,一切都是合成的。然後在viewDidLoad,我有這個。

- (void)viewDidLoad 
{ 
    UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init]; 
    keyboardDoneButtonView.barStyle = UIBarStyleBlack; 
    keyboardDoneButtonView.translucent = YES; 
    keyboardDoneButtonView.tintColor = nil; 
    [keyboardDoneButtonView sizeToFit]; 
    UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done" 
                    style:UIBarButtonItemStyleBordered target:self 
                    action:@selector(pickerDoneClicked:)] autorelease]; 

    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]]; 

    textField.inputAccessoryView = keyboardDoneButtonView; 
    [datePicker removeFromSuperview]; 
    [pickerView removeFromSuperview]; 
    [super viewDidLoad]; 
} 

當文本框被激活,我稱這種

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    [self.view addSubview:pickerView]; 
    [self.view addSubview:datePicker]; 
} 

於是最後,還有的操作方法

- (IBAction)pickerDoneClicked:(id)sender { 
    [datePicker removeFromSuperview]; 
    [pickerView removeFromSuperview]; 
    [textField resignFirstResponder]; 
} 

這一切對我的作品。所有東西都應該顯示和刪除。所以,如果幸運的話,這會做的伎倆對你太

+0

謝謝很多人! – Legolas

+0

完全沒問題。我很高興它幫助你出 – justin

+0

謝謝太slev ..這也適用於我。 +1 –

2
-(void)pickerDoneClicked:(id)sender { 
    [pickerView removeFromSuperview]; 
} 

或者,如果您想用動畫來解除它,請使用UIView動畫更改視圖框架,然後從超級視圖中將其刪除。

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationDelay:1.0]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 

pickerView.frame = outOfScreenFrame; 

[UIView commitAnimations]; 

其中outOfScreenFrame位於您的UIApplication窗口之外。

+0

我已經嘗試過。我得到錯誤無法識別的選擇器發送到實例'1290837' – Legolas

+0

不確定你做錯了什麼,因爲我沒有像slev那樣精確輸入你的代碼,但是我希望你能夠弄明白! :) –

2

在斯威夫特

lazy var inputToolbar: UIToolbar = { 
    var toolbar = UIToolbar() 
    toolbar.barStyle = .Default 
    toolbar.translucent = true 
    toolbar.sizeToFit() 

    var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Bordered, target: self, action: "inputToolbarDonePressed") 
    var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 

    toolbar.setItems([spaceButton, doneButton], animated: false) 
    toolbar.userInteractionEnabled = true 

    return toolbar 
}() 

func inputToolbarDonePressed() { 
    view.endEditing(true) 
} 

UITextFieldDelegate

func textFieldShouldBeginEditing(textField: UITextField) -> Bool { 
    textField.inputAccessoryView = inputToolbar 

    return true 
}