1

我有 2 个函数可以 resignFirstResponder 但当文本字段在滚动视图中时它们都不起作用

我的职能:

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

        if (theTextField == textField1) {
        [textField1 resignFirstResponder];
    }
    if (theTextField == textField2) {
        [textField2 resignFirstResponder];
    }
    if (theTextField == textField3) {
        [textField3 resignFirstResponder];
    }


    return YES;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    [textField1 resignFirstResponder];
    [textField2 resignFirstResponder];
    [textField3 resignFirstResponder];

}

我已经在 IB 中链接了滚动视图,我不知道为什么只有当我在滚动视图之外单击时它才在那里不起作用。所以它只响应视图,但是为什么?我认为[[event allTouches] anyObject] 响应 ANYObjects 上的 ALLtouches

感谢帮助

4

3 回答 3

2

您还可以使用手势识别器来获取背景点击。使用 cancelsTouchesInView = NO 将所有其他触摸转发给正确的接收者。

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTappedBackground:)];
tapGesture.cancelsTouchesInView = NO;
[self.scrollView addGestureRecognizer:tapGesture];
于 2013-02-05T13:41:20.573 回答
1

UIControl将一个大小等于滚动视图继承的透明视图添加到它的最后面然后IBAction为这个新视图创建不是更优雅吗?

于 2011-02-23T11:04:20.863 回答
0
@interface ScrollView <UIScrollViewDelegate,UITextFieldDelegate> {
    UITextField *_currentTF;
}

@implementation

- (void)viewDidLoad {
    yourScrollView.delegate = self;
    yourTextField.delegate = self;
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [_currentTF resignFirstResponder];
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    _currentTF = textField;
    return YES;
}
于 2015-05-09T05:47:02.470 回答