简体   繁体   English

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

[英]Swift: How to use two func textfield at same time

I'm using Xcode 8 and Swift 3. 我正在使用Xcode 8和Swift 3。

I have a project with 3 textfields, 1 button to clear and label to display result. 我有一个项目有3个文本字段,1个按钮清除和标签显示结果。

Inside my class ViewController I have: 在我的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!

I want to limit my textfields inputs to max 3 digits but also to a value of 360. I manage to get code for both things and they work if used only one at a time but because they both start with func textfield I can't make them both work together. 我想将我的文本字段输入限制为最多3位数,但也限制为360.我设法获取这两件事的代码,如果一次只使用一个,但是因为它们都以func textfield开头,我无法制作他们都在一起工作。 Do I have to do it in different class? 我必须在不同的班级做吗? I know this is a basic question but its part of the learning process. 我知道这是一个基本问题,但它是学习过程的一部分。

These are the two codes I want to combine: 这些是我想要组合的两个代码:

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
}

and: 和:

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;
    }
}

They are both inside the class ViewController. 它们都在ViewController类中。

Thanks for the help! 谢谢您的帮助!

You need to add an if statement in func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool checking for the current textField like this: 你需要在func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool添加一个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
   }
}

Swift switch statement will do it. Swift switch语句就可以了。

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

If I am not mistaken, here is all you need: 如果我没有记错的话,这就是你所需要的:

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
}

Update: 更新:

Auto focus on a next texfield. 自动对焦于下一个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
}

People didn't understood the question. 人们不明白这个问题。 Actually, all you need is merging your statement as mentioned by javimuu. 实际上,你所需要的只是合并你的陈述,如javimuu所述。

My answer is terrible since javimuu's one is correct but I'm writing it because I don't have enough rep to down vote those people who up votes each other in an useless way. 我的答案很糟糕,因为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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM