禁用UITextField编辑但仍接受触摸 - Swift 3

时间:2017-01-13 15:00:11

标签: ios swift uitextfield uidatepicker

我正在创建一个UITextField作为输入UIDatePicker。我不希望编辑文本字段(复制,粘贴,剪切和选择文本),但我只想在用户触摸UIDatePicker时显示UITextField,并且所选日期将显示在{ {1}}。

我尝试使用UITextField禁用编辑,但是当我触摸时,TextField不会给我回复。

我应该怎么做才能在Swift 3中实现它?

6 个答案:

答案 0 :(得分:4)

防止出现复制/粘贴菜单...

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if dateTextField.isFirstResponder {
        DispatchQueue.main.async(execute: {
            (sender as? UIMenuController)?.setMenuVisible(false, animated: false)
        })
        return false
    }

    return super.canPerformAction(action, withSender: sender)
}

并实施......

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    // show UIDatePicker
    return false
}

这会在有机会出现之前隐藏菜单

答案 1 :(得分:2)

使用func textField(_ textField:UITextField,shouldChangeCharactersIn范围:NSRange,replacementString string:String) - > Bool {retrun bool}代替textFieldShouldBeginEditing。

class ViewController: UIViewController , UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()

        let datePicker = UIDatePicker()
        datePicker.datePickerMode = UIDatePickerMode.date
        textField.tag = 1
        textField.inputView = datePicker
    }

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if textField.tag == 1 {
            textField.text = ""
            return false
        }
        return true
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField.tag == 1 {
            textField.text = ""
            return false
        }
        return true
    }
}

使用名称StopPasteAction.swift创建一个新类

导入UIKit

class StopPasteAction: UITextField {

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

使用Curren textfield

添加班级新课程

enter image description here

答案 2 :(得分:2)

您可以addTargettextField,并在textField委托方法中禁用如下编辑:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var tedtField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()

        tedtField.delegate = self
        tedtField.addTarget(self, action: #selector(respondsToTf), for: .touchDown)
    }

    func respondsToTf()  {

        // 
        print("you can pop-up the data picker here")
    }

    // MARK: - textfield delegate
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

        return false
    }

}

答案 3 :(得分:1)

这就是我解决这个问题的方式

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var testTxf: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    testTxf.delegate = self
    let tap = UITapGestureRecognizer(target: self, action: #selector(testFunc))
    testTxf.addGestureRecognizer(tap)
    testTxf.isUserInteractionEnabled = true
}

@objc func testFunc() {
    print("tap")
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    return false
}

}

答案 4 :(得分:0)

此外,您可以创建UITextField的子类并实现以下方法:

var parent = Context.Set<Parent>()
    .Where(o => o.Id == Guid.Parse(parentId))
    .Include(o => o.Children)
    .SingleOrDefault();

Context.Entry(parent).Collection(e => e.Children)
    .Query().OfType<RichChild>()
    .Include(e => e.OffshoreAccounts)
        .ThenInclude(e => e.AccountInfo)
    .Load();

答案 5 :(得分:0)

迅捷3:

class MyViewController: UIViewController, UITextFieldDelegate {

   @IBOutlet weak var myTextField           : UITextField!

   override func viewDidLoad() {
      self.myTextField.delegate     = self
   }

   func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
      if textField == myTextField {
         // code which you want to execute when the user touch myTextField
      }
      return false
   }
}