0

ユーザーが「キャンセル」UIBarButtonItemを押したときにキーボードを閉じようとしています。ただし、キャンセルボタンをクリックすると、「認識されないセレクターがインスタンスに送信されました」というエラーが表示されたSIGABRTが表示されます。

キャンセルボタンを作成するための私のコードは次のとおりです。

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    //Add cancel button to navigation bar
    UIBarButtonItem *dismissKeyboardBttn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissKeyboard:)];
    self.navigationItem.rightBarButtonItem = dismissKeyboardBttn;
}

そして、キーボードを閉じるために、私はこの方法を持っています:

- (void)dismissKeyboard:(id)sender
{
    [activeField resignFirstResponder];
    //^^This line causes the SIGABRT^^
}

それはかなり簡単なようです。何か案は?

更新:activeFieldは、ユーザーが現在編集しているUITextFieldにscrollViewを移動するために使用しているUITextFieldです。これは、次の2つの方法で設定されます。

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    activeField = textField; 
}
- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    activeField = nil; 
}

更新2:興味深いことに、キーボード通知を受信するようにViewControllerを登録しました。「textFieldShouldReturn」メソッドを使用してキーボードを閉じようとすると、同じエラーが発生します。これが私のtextFieldShouldReturnコードです:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    if ([textField canResignFirstResponder])
    {
        [textField resignFirstResponder];
    }

    return YES;
}
4

3 に答える 3

1

アクティブフィールドとは?UIResponder の場合は、resignFirstResponder に応答する必要があります。そうではないかもしれません。UIViews と UIViewControllers は UIResponder です。

于 2011-10-06T15:50:03.060 に答える
1

私は状況にあり、現在のView Controllerで次のようにしています:

ヘッダー ファイルで、最初のレスポンダーとなるテキスト フィールドの IBAction を作成し、キーボードを表示します。

- (IBAction)textFieldDidBeginEditing:(UITextField *)textField;

実装ファイルで、バー ボタン (私の場合は "Done" ボタン) を作成し、それをナビゲーション バーの右側に追加するメソッドを作成します。同時に、TextField 間のターゲット アクション ペアを作成します (これがファーストレスポンダーになりました)。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    // create new bar button with "Done" as text
    // set the target of the action as the text field (since we want the text field to resign first responder status and dismiss the keyboard)
    // tell the text field to resign with the stock 'resignFirstResponder' selector
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                         target:textField
                                                                         action:@selector(resignFirstResponder)];

    // add the button with target/action pairing to the navigation bar
    [[self navigationItem] setRightBarButtonItem:bbi];
}

さらに、ボタンをクリックした後にボタンを非表示にしたい (キーボードが消えた) 場合は、textFieldDidEndEditingを使用します。これは、最初のレスポンダーの認識で編集が完了したためです。

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [[self navigationItem] setRightBarButtonItem:nil];
}
于 2012-12-19T03:25:50.007 に答える
0

モーニングスターは正しい(UIButton*)です。また、次の場合は常にこれを追加しresignFirstResponderます。

if(myObject canResignFirstResponder){

}
于 2011-10-06T16:14:05.733 に答える