resignFirstResponder in Swift

2019-04-19 07:06发布

How can I implement it in the new language of Apple:

Objective-C code:

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

I have tried to do so. But the keyboard does not disappear:

Swift code:

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

2条回答
We Are One
2楼-- · 2019-04-19 07:30

Try this

self.view.endEditing(true)
查看更多
爷、活的狠高调
3楼-- · 2019-04-19 07:35

You could probably go with:

self.view.endEditing(true)
查看更多
登录 后发表回答