Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在uitableviewcell中辞去textfield作为第一响应者的职务_Iphone_Objective C_Cocoa Touch_Uitableview_Uitextfield - Fatal编程技术网

Iphone 如何在uitableviewcell中辞去textfield作为第一响应者的职务

Iphone 如何在uitableviewcell中辞去textfield作为第一响应者的职务,iphone,objective-c,cocoa-touch,uitableview,uitextfield,Iphone,Objective C,Cocoa Touch,Uitableview,Uitextfield,当使用标准的UIView时,我使用此代码将我的UITextField改为firstResponder 但是我现在在UITableView中的UITextField单元格中有UITextField,当我在文本字段外单击时,代码不会将文本字段作为第一响应者。有没有办法让这一切顺利进行 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches]

当使用标准的
UIView
时,我使用此代码将我的
UITextField
改为
firstResponder

但是我现在在
UITableView
中的
UITextField
单元格中有
UITextField
,当我在文本字段外单击时,代码不会将文本字段作为第一响应者。有没有办法让这一切顺利进行

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];
    if ([self.temperatureTextField isFirstResponder] && [touch view] != self.temperatureTextField) {
        [self.temperatureTextField resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}

[[self tableView]编辑:是]
是我的标准方法。

为了补充Matt Wilding对UITableView特定案例的回答,我使用的方法如下: 我们通常希望在两种情况下隐藏键盘:点击文本UI元素外部,或向下/向上滚动UITableView。第一个场景可以通过TapGestureRecognitor轻松添加,第二个场景通过UIScrollViewDelegate scrollViewWillBeginDragging:方法添加。 首先,隐藏键盘的方法:

   /**
     *  Shortcut for resigning all responders and pull-back the keyboard
     */
    -(void)hideKeyboard
    {
        //this convenience method on UITableView sends a nested message to all subviews, and they resign responders if they have hold of the keyboard
        [self.tableView endEditing:YES];

    }
此方法将调整UITableView视图层次结构中子视图的任何textField UI,因此它比单独调整每个元素更实用

下一步,我们将通过外部轻击手势处理解雇事宜,包括:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupKeyboardDismissGestures];

}

- (void)setupKeyboardDismissGestures
{

//    Example for a swipe gesture recognizer. it was not set-up since we use scrollViewDelegate for dissmin-on-swiping, but it could be useful to keep in mind for views that do not inherit from UIScrollView
//    UISwipeGestureRecognizer *swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
//    swipeUpGestureRecognizer.cancelsTouchesInView = NO;
//    swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
//    [self.tableView addGestureRecognizer:swipeUpGestureRecognizer];

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    //this prevents the gestureRecognizer to override other Taps, such as Cell Selection
    tapGestureRecognizer.cancelsTouchesInView = NO;
    [self.tableView addGestureRecognizer:tapGestureRecognizer];

}
将tapGestureRecognizer.cancelsTouchesInView设置为否是为了避免gestureRecognizer覆盖UITableView的正常内部工作(例如,不干扰单元格选择)

最后,要在向上/向下滚动UITableView时隐藏键盘,我们必须实现UIScrollViewDelegate协议ScrollViewWillBeginDraging:方法,如下所示:

.h文件

@interface MyViewController : UIViewController <UIScrollViewDelegate>

我希望这会有帮助!=)

@Faisal,我想辞职的地方,第一反应者。它因情况不同而不同。在您的情况下,假设您的touchsbegind:withEvent正在被调用,将其放在那里应该会起作用。我尝试过这样做,但当我在
UITextField
之外单击时,无法将其关闭。当
UITextField
是独立的并且不在
UITableViewCell
@Faisal内部时,我发布的代码工作正常,在这种情况下,可能是触摸事件的传递出了问题。我猜这个方法从来没有被调用过?实际上我把你的代码添加到了我的
didSelectRowAtIndexPath
中,效果很好。
#pragma mark - UIScrollViewDelegate

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self hideKeyboard];
}