2011-06-28 29 views
4

これは、ナビゲーションバーのボタン、またはピッカービューの上にあるツールバーの「完了」ボタンであるUIPickerViewを破棄することをお勧めします。私は両方のボタンを実装して、私はピッカーの表示を却下し、最初のレスポンダーを辞任しようとしています。UIToolBarの完了ボタンを使用して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

私のテストアプリケーションはずっとシンプルですが、構造がまだあなたのために働いていることを確信しています。

本質的に、これは私がやったすべてです。私はUIPickerView,UIDatePickerViewUITextFieldをIBに設定しています。 pickerViewのdataSourcedelegateは、FileFieldのdelegateと同じように、File's Ownerにリンクされています。私のヘッダーで

は、私はそれらのすべては、私もリンクされたプロトコルを持って次のような構造

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 
} 
関連する問題