UitextField resignFirstResponder在滚动视图中不起作用

时间:2011-02-23 10:48:37

标签: objective-c cocoa-touch uiscrollview uitouch

我有两个函数来resignFirstResponder但是当文本字段在scrollview中时它们都不起作用

我的职能:

-(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中的滚动视图,我无法找到为什么它不能在那里工作只有当我点击scrollview.so它只响应视图,但为什么?我认为[[event allTouches] anyObject]响应到任何对象的ALLtouches

感谢您的帮助

3 个答案:

答案 0 :(得分:2)

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

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTappedBackground:)];
tapGesture.cancelsTouchesInView = NO;
[self.scrollView addGestureRecognizer:tapGesture];

答案 1 :(得分:1)

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

答案 2 :(得分: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;
}