代码之家  ›  专栏  ›  技术社区  ›  Enamul Haque

在swift 4中打开子视图之前如何删除键盘?

  •  0
  • Enamul Haque  · 技术社区  · 6 年前

    我用下面的代码打开子视图。。

     func btnWatisPlan() {
        let popUpVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Category") as! Category
        self.addChildViewController(popUpVc)
        popUpVc.view.frame = self.view.frame
        self.view.addSubview(popUpVc.view)
        popUpVc.didMove(toParentViewController: self)
    }
    

    这是我的 textFieldDidbginEditing 代码&from textfield didbginediting我将子视图调用到我的textfield名为

      func textFieldDidBeginEditing(_ textField: UITextField) {        
        if (textField == sp_whatis_for_plan)
        {
    
           view.endEditing(true)
            btnWatisPlan();
    
        }
     }
    

    请帮帮我。。

    4 回复  |  直到 6 年前
        1
  •  0
  •   Fa.Shapouri    6 年前
    textField.resignFirstResponder()
    

        2
  •  0
  •   kathayatnk    6 年前

     func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if textField == sp_whatis_for_plan {
            btnWhatIsPlan()  //call the popup here
            view.endEditing(true) //end the editing on the view
            return false  //also return false
        }
        return true
    } 
    
        3
  •  0
  •   Ashley Mills    6 年前

    文本字段应开始编辑 它表示是否允许键盘输入特定文本。回来吧

    请尝试下面的代码

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    
        if textField == sp_whatis_for_plan
        {
            btnWatisPlan();
            return false  
        }
        return true  
    }
    
        4
  •  0
  •   Kamaldeep singh Bhatia    6 年前

    或者你可以 对于父视图。

    辞职第一反应者

    func textFieldDidBeginEditing(_ textField: UITextField) {        
        if (textField == sp_whatis_for_plan){
           textField.resignFirstResponder()
            btnWatisPlan();
        }
     }
    

    结束编辑

    func textFieldDidBeginEditing(_ textField: UITextField) {        
            if (textField == sp_whatis_for_plan) {
               self.view.resignFirstResponder()
               //Note: the view must be the view which contains the text field
               btnWatisPlan();
            }
         }
    

    **

    启用您可以添加 @IBAction 在里面做同样的动作请参考图片 Interface builder for UITextField

    **