简体   繁体   English

控制 - 拖动以在Xcode 5中创建IBAction以关闭键盘?

[英]Control-drag to create IBAction in Xcode 5 to dismiss keyboard?

I have a view controller with a text field. 我有一个带有文本字段的视图控制器。 I'm trying to create an IBAction to dismiss they keyboard when user taps outside the text field by control-dragging from outside the text field in the Xib file to my .h file, but I don't get the option to choose "Action," only "Outlet" and "Outlet collection." 当用户通过控制从Xib文件中的文本字段外部拖动到我的.h文件时,我正在尝试创建一个IBAction来关闭它们键盘,但是我没有选择“Action”的选项,“只有”Outlet“和”Outlet collection。“ I don't even get a window that asks me to choose when I control drag inside a *.m file. 当我在* .m文件中控制拖动时,我甚至没有一个窗口要求我选择。 What am I missing? 我错过了什么?

I was able to do this in Xcode 4. 我能够在Xcode 4中做到这一点。

When you ctrl-drag from "outside the text field", you are most likely interacting with the top-level view of the view controller. 从“文本字段外”按住Ctrl键拖动时,很可能与视图控制器的顶级视图进行交互。 This is a UIView object which does not have actions. 这是一个没有动作的UIView对象。 Only views that inherit from UIControl have actions, eg buttons, text fields. 只有从UIControl继承的视图才有动作,例如按钮,文本字段。 This is not an XCode 4 vs 5 issue -- it's always been this way. 这不是XCode 4 vs 5的问题 - 它总是这样。

In your view controller, override touchesBegan:withEvent: . 在视图控制器中,覆盖touchesBegan:withEvent: . Make a call to [self.view endEditing] to dismiss the keyboard. 拨打[self.view endEditing]以关闭键盘。

This is possible because your view controller inherits from the UIResponder class, as do UIViews . 这可能是因为您的视图控制器从继承UIResponder类,因为这样做UIViews When the user touches something on the screen, the event gets passed up the responder chain, eg from subview to parent view to parent view controller etc., until one of those responders decides to respond to or purposefully discard that event. 当用户触摸屏幕上的某些内容时,事件被传递到响应者链,例如从子视图到父视图到父视图控制器等,直到其中一个响应者决定响应或有目的地丢弃该事件。 By overriding touchesBegan:withEvent: , your view controller can handle that event and do something meaningful (like dismiss the keyboard). 通过覆盖touchesBegan:withEvent: ,您的视图​​控制器可以处理该事件并执行一些有意义的操作(例如关闭键盘)。

A simple solution would be using a simple UITapGestureRecognizer added to the "main" view. 一个简单的解决方案是使用添加到“主”视图的简单UITapGestureRecognizer。

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

Then the method hideKeyboard: 然后方法hideKeyboard:

- (void)hideKeyboard
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
} <br>

The tap is sent through views until it finds one that could manage it, like the view controller view that triggers the gesture. 点击通过视图发送,直到找到可以管理它的视图,例如触发手势的视图控制器视图。

Use this code on your .m file 在.m文件中使用此代码

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

        selectedTextField = textField;
        return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [selectedTextField resignFirstResponder];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM