当我们点击iOS swift中的文本字段时,是否可以显示操作表并隐藏键盘?

时间:2017-08-14 12:50:26

标签: ios xcode swift3 uitextfield

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

3 个答案:

答案 0 :(得分:1)

试试这个它应该适合你,首先为textfield设置委托。

 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:

  

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

答案 1 :(得分:1)

使用UITextFieldDelegate:

1-将代表添加到您的文本字段:

yourTextField.deleagete = self

2-像这样使用委托

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

答案 2 :(得分:0)

斯威夫特 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
}