0

重複の可能性:
UITextFieldの外部の任意の場所でタッチ時にキーボードを閉じる

Interface BuilderのMyViewController:

ここに画像の説明を入力してください

カスタムTableViewCellには、TextFieldがあります。My plan is disappear KeyPad when User Clicks outside the touchpad.

私はそれを使用して動作させることができませんでしたUITapGestureRecognizer

viewDidLoadで:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];

では、どうすればそれを機能させることができcell.TextFieldますか?

私はここでこの解決策について読みました:

[self.view endEditing:YES];

また

[[self.tableView superView] endEditing];

私もそれを機能させることができませんでした。どこで使えばいいですか?

または、もっと良い方法があれば、私に知らせてください。

編集

私はそれを機能させることができませんでした:

@property (nonatomic, strong) UITextField *cellTextField;

viewDidLoadで:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.settingTableView addGestureRecognizer:gestureRecognizer];

そして、私たちは持っています:

- (void) hideKeyboard {
    [self.cellTextField resignFirstResponder];
}

と :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


    self.cellTextField = cell.dataEdit;

私のせいは何ですか?

4

5 に答える 5

0

画面全体にカスタムUIButtonを配置すると、キーボードを非表示にできます。次に、このカスタムUIButtonを使用してこのメ​​ソッドをアタッチします。このメソッドを実装する前に、InterfaceBuilderでUITextFieldタグ=1を設定します。

-(IBAction)HideKeyboard
{
    [[self.view viewWithTag:1]resignFirstResponder];
}

注:CustomCellをクリックしてキーボードを非表示にする場合は、このUIButtonをそこに配置するか、カスタムUIButtonを使用せずにコードで行ったように、ジェスチャ認識を使用してこのメ​​ソッドを呼び出すことができます。また、UITextFieldデリゲートを接続していることを確認してください。この作業を希望します。

于 2012-11-12T16:42:08.143 に答える
0

View Controllerにプロパティを追加できます。

@property (nonatomic, strong) UITextField *cellTextField

次に、 cell.TextField をそれに割り当てます。

hideKeyboard:キーボードを閉じることができます

[cellTextField resignFirstResponder];

resignFirstResponderコントローラーのビューではなく、textField 自体を呼び出す必要があります。

于 2012-11-12T15:40:14.030 に答える
0

これがうまくいくことを願っ[cellTextField resignFirstResponder];ています...

多分UITextFieldDelegateを見て、特に- (BOOL)textFieldShouldReturn:(UITextField *)textField

于 2012-11-12T15:42:29.847 に答える
0

これを試すことができます。

- (void) hideKeyboard {
    [self.view endEditing:YES];
}
于 2012-11-12T18:20:35.973 に答える
0

次のメソッドを挿入することで、このタスクを達成できます

UITextFieldDelegate を実装する必要があります

ストーリーボードまたはテーブルのセルの読み込みを開始するときに、テキスト フィールドのタグを設定する必要があります。

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.currentIndexPath = [(UITableView*)textField.superview.superview.superview indexPathForCell:(UITableViewCell*)textField.superview.superview];
    self.currentTableView = (UITableView*)textField.superview.superview.superview;
    self.keyboardIsShown = YES;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    if(self.keyBoardIsShown)
    {
        [self dismissKeyboard:currentPoint UsedTableView:self.myTableView IndexPathForRow:self.currentIndexPath TexFieldTag:tag];
        self.keyBoardIsShown = NO;
    }
}

-(void)dismissKeyboard:(CGPoint)currentPoint UsedTableView:(UITableView*)table IndexPathForRow:(NSIndexPath*)indexPath TexFieldTag:(int)tag
{
    UITableViewCell *cell = [table cellForRowAtIndexPath:indexPath];
    if(!CGRectContainsPoint([self.view convertRect:[cell viewWithTag:tag].frame fromView:[cell viewWithTag:tag].superview.superview], currentPoint))
    {
        [(UITextField*)[cell viewWithTag:tag] resignFirstResponder];
    }
}
于 2012-11-13T08:34:47.233 に答える