UITextfield委托响应所有文本字段

时间:2015-11-17 13:55:44

标签: ios swift

我有以下代码

var departureField = UITextField()
var arrivalField = UITextField()
var dateField = UITextField()

我正在设置arrivalField的委托,而不是其他字段的委托

arrivalField.delegate = self

我有以下委托方法textFieldShouldBeginEditing。当我点击departureField或dateField时,两者都响应该方法。我只为到达字段设置了委托,我只想回复到达文本字段。我完全错过了什么吗?

import Foundation
import SwiftDate

class FlightStatusRouteViewController: UIViewController, UITextFieldDelegate {

    var date = NSDate()

    var departureField: UITextField!
    var arrivalField: UITextField!
    var dateField: UITextField!

    override func viewDidLoad() {
        self.createUI(self.view)
    }

    func createUI(parent: UIView) {
        let height = CGFloat(45)
        let datePicker = UIDatePicker()
        let btn = createButton("Submit")

        departureField = self.createInput("Departure Airport")
        arrivalField = self.createInput("Arrival Airport")
        dateField = self.createInput("Departure Date")

        departureField.delegate = nil
        arrivalField.delegate = self
        dateField.delegate = nil

        datePicker.datePickerMode = UIDatePickerMode.Date
        dateField.inputView = datePicker
        datePicker.addTarget(self, action: "updateDate:", forControlEvents: UIControlEvents.ValueChanged)

        parent.addSubview(departureField)
        parent.addSubview(arrivalField)
        parent.addSubview(dateField)
        parent.addSubview(btn)

        self.createTapGesture(btn, action: "submitForm:")

        dateField.text = date.toString(format: DateFormat.Custom("EEEE, dd MMMM YYYY"))

        dateField.snp_makeConstraints{ (make) -> Void in
            make.left.equalTo(parent)
            make.right.equalTo(parent)
            make.top.equalTo(parent).offset(10)
            make.height.equalTo(height)
        }

        departureField.snp_makeConstraints{ (make) -> Void in
            make.left.equalTo(parent)
            make.right.equalTo(parent)
            make.height.equalTo(height)
            make.top.equalTo(dateField.snp_bottom).offset(10)
        }

        arrivalField.snp_makeConstraints{ (make) -> Void in
            make.left.equalTo(parent)
            make.right.equalTo(parent)
            make.top.equalTo(departureField.snp_bottom).offset(10)
            make.height.equalTo(height)
        }

        btn.snp_makeConstraints{ (make) -> Void in
            make.top.equalTo(arrivalField.snp_bottom).offset(20)
            make.left.equalTo(parent)
            make.right.equalTo(parent)
            make.height.equalTo(height)
        }
    }

    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        let searchVc = SearchAirportsTableViewController()
        searchVc.searchField = textField
        self.presentViewController(searchVc, animated: true, completion: nil)
        return false
    }

    internal func updateDate(sender: UIDatePicker) {
        date = sender.date
        self.dateField.text = sender.date.toString(format: DateFormat.Custom("EEEE, dd MMMM YYYY"))
    }
}
func createInput(placeholder: String) -> UITextField {
    let textfield = TextField()
    let fonts = FlySAAFonts()

    textfield.placeholder = placeholder
    textfield.layer.cornerRadius = 5
    textfield.layer.borderWidth = 1
    textfield.font = UIFont(name: fonts.std, size: 14)!
    textfield.backgroundColor = FlySAAColors.offWhite
    textfield.tintColor = FlySAAColors.darkGry
    textfield.borderStyle = UITextBorderStyle.None
    textfield.layer.borderWidth = 0

    return textfield
}

3 个答案:

答案 0 :(得分:0)

您可以在故事板上查看插座。

textFieldShouldBeginEditing

你检查是否

textField == departureField || textField == dateField
return false

答案 1 :(得分:0)

是的,这是理想的行为。在委托方法中,您可以比较文本字段,以了解您希望如何根据字段进行操作。 if textField == departureTextField然后做你想做的事。

答案 2 :(得分:0)

我正在使用IQKeyboardManager,我从未正确阅读过README.md。可以找到解决方案here