Swift:如何同时使用两个func文本字段

时间:2017-02-16 09:37:15

标签: ios swift

我正在使用Xcode 8和Swift 3。

我有一个包含3个文本字段的项目,1个用于清除和标记的按钮以显示结果。

在我的ViewController类中,我有:

class ViewController: UIViewController,UITextFieldDelegate {

@IBOutlet weak var input1: UITextField!
@IBOutlet weak var input2: UITextField!
@IBOutlet weak var input3: UITextField!
@IBOutlet weak var lblResult: UILabel!
@IBOutlet weak var clearButton: UIButton!

我希望将我的文本字段输入限制为最多3位数,但也限制为360.我设法获取两种情况的代码,如果一次只使用一种,它们可以工作但是因为它们都以func textfield开头我可以& #39;让它们一起工作。我必须在不同的班级做吗? 我知道这是一个基本问题,但它是学习过程的一部分。

这是我想要合并的两个代码:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, 
               replacementString string: String) -> Bool
{
    let maxLength = 3
    let currentString: NSString = textField.text! as NSString
    let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString
    return newString.length <= maxLength
}

func textField(_ textField: UITextField,
               shouldChangeCharactersIn range: NSRange,
               replacementString string: String) -> Bool
{
    var startString = ""
    if (textField.text != nil)
    {
        startString += textField.text!
    }
    startString += string
    let limitNumber = Int(startString)
    if limitNumber! > 360
    {
        return false
    }
    else
    {
        return true;
    }
}

它们都在ViewController类中。

感谢您的帮助!

4 个答案:

答案 0 :(得分:1)

您需要在if statement中添加func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool,检查当前textField,如下所示:

func textField(_ textField: UITextField,  shouldChangeCharactersIn range: NSRange,  replacementString string: String) -> Bool {
   if textFiled == input1 {
   // do logic for input1
   } else if textFiled == input2 {
   // do logic for input2
   }
}

答案 1 :(得分:1)

Swift switch语句将会这样做。

func textField(_ textField: UITextField,
               shouldChangeCharactersIn range: NSRange,
               replacementString string: String) -> Bool
{
    switch textField {
    case input1:
        // ...
    case input2:
        // ...
    case input3:
        // ...
    default:
        break
    }
}

答案 2 :(得分:1)

如果我没有记错的话,这就是你所需要的:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, 
               replacementString string: String) -> Bool {
    let maxLength = 3
    let limitValue = 360

    let text = textField.text!
    let currentString: NSString = text as NSString
    let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString

   var startString = ""

    if !text.isEmpty {
        startString += text
    }
    startString += string

    let limitNumber = Int(startString)!

    return limitNumber < limitValue && newString.length <= maxLength
}

<强>更新

自动对焦于下一个texfield。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, 
                   replacementString string: String) -> Bool {
    let maxLength = 3
    let limitValue = 360

    let text = textField.text!
    let currentString: NSString = text as NSString
    let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString

   var startString = ""

    if !text.isEmpty {
        startString += text
    }
    startString += string

    let limitNumber = Int(startString)!

    let newLength: Int = newString.length

    if textField == input1 {
        if newLength == maxLength {
            input2.becomeFirstResponder()
        }
    }
    if textField == input2 {
        if newLength == maxLength {
            input3.becomeFirstResponder()
        }
    }

    if textField == input3 {
        if newLength ==  maxLength {
           self.view.endEditing(true)
        }
    }

    return limitNumber < limitValue && newLength <= maxLength
}

答案 3 :(得分:0)

人们没有理解这个问题。实际上,你所需要的只是合并你的陈述,如javimuu所述。

我的答案很糟糕,因为javimuu的一个是正确的,但我写的是因为我没有足够的代表来投票那些以无用的方式互相投票的人。

  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange,
               replacementString string: String) -> Bool
{
    let maxLength = 3
    let currentString: NSString = textField.text! as NSString
    let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString

    var startString = ""
    if (textField.text != nil)
    {
        startString += textField.text!
    }
    startString += string
    let limitNumber = Int(startString)


    return newString.length <= maxLength && limitNumber! <= 360
}