最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]swift判断文字输入框获得焦点

Swift crifan 1529浏览 0评论

swift textfield focus

ios – focusing a text field in swift – Stack Overflow

swift textfield focus event

iphone – Detect Focus Change for UITextField – Stack Overflow

textFieldDidBeginEditing

textFieldDidEndEditing

然后去看看原型:

public protocol UITextFieldDelegate : NSObjectProtocol {
   
    @available(iOS 2.0, *)
    optional public func textFieldShouldBeginEditing(textField: UITextField) -> Bool // return NO to disallow editing.
    @available(iOS 2.0, *)
    optional public func textFieldDidBeginEditing(textField: UITextField) // became first responder
    @available(iOS 2.0, *)
    optional public func textFieldShouldEndEditing(textField: UITextField) -> Bool // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
    @available(iOS 2.0, *)
    optional public func textFieldDidEndEditing(textField: UITextField) // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
   
    @available(iOS 2.0, *)
    optional public func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool // return NO to not change text
   
    @available(iOS 2.0, *)
    optional public func textFieldShouldClear(textField: UITextField) -> Bool // called when clear button pressed. return NO to ignore (no notifications)
    @available(iOS 2.0, *)
    optional public func textFieldShouldReturn(textField: UITextField) -> Bool // called when ‘return’ key pressed. return NO to ignore.
}

去写代码试试

果然是可以的:

    func textFieldDidBeginEditing(textField: UITextField) {
//        print("textField=\(textField)")
//        //<UITextField: 0x7b9a3b10; frame = (25 225.5; 270 40); text = ‘11111111111′; clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0x7a74d870>; layer = <CALayer: 0x7b9a5510>>
    }
   
    func textFieldDidEndEditing(textField: UITextField) {
        print("textField=\(textField)") 
    }

分别对应着:

点击开始编辑文本

编辑完毕离开文本

[总结]

想要获得文本的输入和离开的事件,对应着获得和失去焦点,可以用对应的

UITextFieldDelegate的代理函数:

func textFieldDidBeginEditing(textField: UITextField)
func textFieldDidEndEditing(textField: UITextField)

转载请注明:在路上 » [已解决]swift判断文字输入框获得焦点

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
58 queries in 0.080 seconds, using 18.57MB memory