Swift中的resignFirstResponder

时间:2014-06-04 10:16:40

标签: uiview swift ios8

如何使用Apple的新语言实现它:

Objective-C代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        for (UIView *view in self.view.subviews)
        [view resignFirstResponder];
}

我试过这样做。但键盘并没有消失:

Swift代码:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event)
    self.view.resignFirstResponder()
}

2 个答案:

答案 0 :(得分:47)

你可以选择:

self.view.endEditing(true)

答案 1 :(得分:3)

试试这个

self.view.endEditing(true)