【问题标题】:is it possible to show action sheet and hide keyboard when we click on a textfield in iOS swift?当我们在 iOS swift 中单击文本字段时,是否可以显示操作表和隐藏键盘?
【发布时间】:2018-01-22 07:36:47
【问题描述】:

当我单击文本字段时,我试图从操作表中获取文本字段的值。单击文本字段时,我不想显示键盘。 实际上我需要 uitextfield 中的按钮单击属性是否可以这样做?

【问题讨论】:

  • 为什么不让按钮看起来像一个文本字段?触摸按钮并显示操作表。
  • @JefferyThomas 我需要天空浮动文本字段本身
  • @JefferyThomas 我想在文本字段上方放置一个按钮,有任何简单的方法可以将两者放在相同的约束条件下
  • 是的,您可以将约束放在一个上(不管哪个),第二个约束到第一个(从上到上、从左到左、从下到下、从右到右)。这样,当对主要约束的约束发生变化时,另一个会自动适应一次。

标签: ios xcode swift3 uitextfield


【解决方案1】:

试试这个它应该适合你,首先为文本字段设置委托。

 func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
            textField.resignFirstResponder()
            let actionSheet = UIActionSheet(title: "Select", delegate: self, cancelButtonTitle: "Yes", destructiveButtonTitle: "", otherButtonTitles: "A", "B")
            actionSheet.actionSheetStyle = .default
            actionSheet.show(in: self.view)
        return false
    }

textFieldShouldBeginEditing:

当用户执行一个动作时,文本域调用这个方法 通常会启动文本字段文本的编辑。

【讨论】:

    【解决方案2】:

    使用 UITextFieldDelegate:

    1- 将委托添加到您的文本字段:

    yourTextField.deleagete = self
    

    2- 像这样使用委托

    func textFieldDidBeginEditing(textField: UITextField) {
            textField.resignFirstResponder()
            //call your function here
        }
    

    【讨论】:

      【解决方案3】:

      斯威夫特 4

      private var positionArray = ["All", "QB", "RB", "WR", "TE", "K", "DE", "OL", "LB", "EDGE", "DT", "CB", "S"]
      

      // 1 在您的文本字段中添加一个委托

      tf.delegate = self
      

      // 2 扩展你的 UITextfield 委托

        func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
      
         // to avoid keyboard
          textField.resignFirstResponder()
      
        // add action sheet
          let alert = UIAlertController(title: LocalString("draft_title"), message: LocalString("draft_select_position"), preferredStyle: .actionSheet)
              
           for i in positionArray {
              alert.addAction(UIAlertAction(title: i, style: .default , handler:{ (UIAlertAction)in
                  textfield.text = i
                  self.view2021Draft?.positionSelected(isSelected: true, positionValue: textfield.text ?? "")
              }))
          }
          
          alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler:{ (UIAlertAction)in
              self.view2021Draft?.positionSelected(isSelected: false, positionValue: textfield.text ?? "")
          }))
          
          //uncomment for iPad Support
          alert.popoverPresentationController?.sourceView = self.view
          
          self.present(alert, animated: true, completion: {
              print("completion block")
          })
          return false
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-26
        • 2014-10-17
        • 1970-01-01
        • 2015-08-20
        • 1970-01-01
        • 2020-07-12
        • 1970-01-01
        相关资源
        最近更新 更多