2015-04-29 96 views
1

我有一个UIViewController其中UITextField在里面,我试图解雇键盘时,我点击或视图被解雇。但是,当我拨打resignFirstResponder()时,键盘仍然不排除,我不确定为什么。这里是我的代码:UITextField键盘没有解雇

class MessageViewController: UIViewController, UITextFieldDelegate { 
    var messageTextField : UITextField = UITextField() 

    override func viewDidLoad() { 
     ... 
     messageTextField.frame = CGRectMake(10, UIScreen.mainScreen().bounds.size.height-50, UIScreen.mainScreen().bounds.size.width-80, 40) 
    messageTextField.delegate = self 
    messageTextField.borderStyle = UITextBorderStyle.Line 
    messageTextField.becomeFirstResponder() 
    self.view.addSubview(messageTextField) 

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil) 
     ... 
    } 

    func textFieldShouldReturn(textField: UITextField) -> Bool { 
     textField.resignFirstResponder() 
     return true 
    } 

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
     self.view.endEditing(true) 
     println("Touched") 
    } 

    func keyboardWillShow(notification: NSNotification) { 
     var keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey]as? NSValue)?.CGRectValue() 
     let messageFrame = messageTextField.frame 
     let newY = messageFrame.origin.y - keyboardSize!.height 
     messageTextField.frame = CGRectMake(messageFrame.origin.x, newY, messageFrame.size.width, messageFrame.size.height) 
    } 

    func keyboardWillHide(notification: NSNotification) { 
     var keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey]as? NSValue)?.CGRectValue() 
     let messageFrame = messageTextField.frame 
     let newY = messageFrame.origin.y - keyboardSize!.height 
     messageTextField.frame = CGRectMake(messageFrame.origin.x, newY, messageFrame.size.width, messageFrame.size.height) 
    } 

} 

有谁知道为什么键盘没有解雇?我以编程方式将UITextField添加到视图,而不是使用情节提要。这有什么区别吗?

+0

使用回[文本字段resignFirstResponder]; –

+0

使用TapGesturerecognizer – vijeesh

+0

@AshokLondhe我用self.view.endEditing(true)和textField.resignFirstResponder()。都没有工作。 – user1998511

回答

2
class ViewController: UIViewController,UITextFieldDelegate { 

确认协议

messageTextField.delegate=self 

组委托

func textFieldShouldReturn(textField: UITextField!) -> Bool 
    { 
     messageTextField.resignFirstResponder() 
     return true; 
    } 

使用此代码..

+0

接受答案,如果它适合你 – vijeesh

+0

它没有为我工作 – user1998511

+0

类ViewController:UIViewController,UITextFieldDelegate你符合协议? – vijeesh

1

以下是我做到了这个问题。我希望这种方法能解决你的问题。

文本框

@IBOutlet var userID : UITextField! 

功能。

func textFieldShouldReturn(textField: UITextField!)-> Bool 
{ userID.resignFirstResponder() 
    return true 
} 

在你想要的功能中,你需要编写这个语法。

@IBAction func login(sender: AnyObject) 
{ 
    userID.resignFirstResponder() 
} 

这是在viewDidLoad中或ViewDidAppear

override func viewDidLoad() 
{ 
    userID.delegate = self; 
} 

我不能对以前的用户评论。这就是我写这个的原因。 我希望这给你的想法。

0

@ vijeesh的答案可能会起作用,逻辑几乎是正确的,但如果您使用多个UITextField,则在技术上是错误的。 textField是调用textFieldShouldReturn时传递的UITextField参数。问题是,你只是宣称:

textField.resignFirstResponder() 

你的程序不知道是什么的UITextField你指的是。即使您的程序中只能有一个textField,并且您知道它是您的messageTextField,但该程序仍然不知道。它只是看到“textField”。所以你必须告诉它在你的程序中为每个UITextField做些什么。即使你只有一个。这应该工作:

func textFieldShouldReturn(textField: UITextField) -> Bool { 
    if textField == messageTextField { 
     messageTextField.resignFirstResponder() 
    } 
    return true 
} 
0

很简单。 第一添加点触手势

var tap = UITapGestureRecognizer(target: self, action: "handle:") 
view.addGestureRecognizer(tap) 

函数处理

func handle(tap: UITapGestureRecognizer){ 
view.endEditing(true) 
} 

这样你就可以当你点击的UITextField外关闭键盘