Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UITextField 与 键盘的关闭 #3

Closed
here-we-goal opened this issue Nov 6, 2013 · 1 comment
Closed

UITextField 与 键盘的关闭 #3

here-we-goal opened this issue Nov 6, 2013 · 1 comment

Comments

@here-we-goal
Copy link
Owner

loadView内代码创建了一个textField,头文件内并没有声明
添加到view中

然后需要对呼出的键盘做一下关闭功能的配置
网上找了一圈,差不多就是在两种场景下需要关闭键盘
一是点击键盘上的return键,一是点击textField以外的区域

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
// 触摸背景,关闭键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    UIView *view = (UIView *)[touch view];
    NSLog(@"touch");
    if(view == self.view){
       // 如果textField使用局部对象,这里就需要采用ViewWithTag的方式来取到textView。从前端的角度来说可能是类似于dom选择器。
       // 更好的方法是将textField控件再头文件进行声明。这样性能更好,代码简洁
        // [self.textField resignFirstResponder];
    }
}
@here-we-goal
Copy link
Owner Author

// 触摸背景,关闭键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    UIView *view = (UIView *)[touch view];
    NSLog(@"touch");
    if(view == self.view){
        [self.textField resignFirstResponder];//第一种方法,再头文件中声明成全局控件
        //[[self.view.subviews objectAtIndex:3] resignFirstResponder];//第二种方法,找到subViews数组中对应的view索引
        /* 第三种方法 遍历subviews找到UITextField类型的view
         * http://stackoverflow.com/questions/10395690/clear-uitextfield-data-when-coming-to-that-page-again-iphone
         */
        /*
        NSArray *arraysubViews = [self.view subviews];
        for(UIView *subView in arraysubViews){
            if([subView isKindOfClass:[UITextField class]]){
                [subView resignFirstResponder];
            }
        }
         */
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant