3 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊
@implementation UIView (FindFirstResponder)- (id)findFirstResponder{ if (self.isFirstResponder) { return self; } for (UIView *subView in self.subviews) { id responder = [subView findFirstResponder]; if (responder) return responder; } return nil;}@end
iOS 7+
- (id)findFirstResponder{ if (self.isFirstResponder) { return self; } for (UIView *subView in self.view.subviews) { if ([subView isFirstResponder]) { return subView; } } return nil;}
斯威夫特
extension UIView { var firstResponder: UIView? { guard !isFirstResponder else { return self } for subview in subviews { if let firstResponder = subview.firstResponder { return firstResponder } } return nil }}
SWIFT中的用法示例:
if let firstResponder = view.window?.firstResponder { // do something with `firstResponder`}

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊
[self.view endEditing:YES]

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
[self.view.window endEditing:YES]
.
- 3 回答
- 0 關(guān)注
- 729 瀏覽
添加回答
舉報(bào)