keyboardWillHide通知故障

时间:2017-06-30 04:28:32

标签: ios swift

我有:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

}

还有:

func keyboardWillShow(notification: NSNotification) {

    if (self.view.center.y == maxPointY){
        self.view.frame.origin.y -= maxPointY/2
    }
}

func keyboardWillHide(notification: NSNotification) {

    self.view.frame.origin.y += maxPointY/2
}

问题是,当我点击应该弹出键盘的文本字段时,keyboardWillHide会被调用。

我也有这个:

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

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

2 个答案:

答案 0 :(得分:1)

试试这个: Swift 3

它在我这边工作。我认为你已经传递了没有参数的选择器

 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

<强>选择

func keyboardWillShow(notification: NSNotification) {
    }

func keyboardWillHide(notification: NSNotification) {
     }

<强>代表

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

答案 1 :(得分:1)

这是一个小问题:

XDocument xd = XDocument.Load(ms);
XElement root = xd.Document.Root;
var list = root.Descendants("Shortcuts").Descendants("Shortcut").Select(x =>
    new Shortcut()
    {
        Id = Convert.ToInt32(x.Attribute("Id").Value),
        TypeOfLink = GetTypeFromString(x.Descendants("Type")
                                        .First().Value),
        FullPathToTarget = x.Descendants("FullPath")
                                        .First().Value,
        Name = x.Descendants("Name").First().Value
        }).ToList();

我没有打电话给override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { self.view.endEditing(true) } 。我不得不把它改成:

super.touchesBegan