避免为很多按钮复制代码

时间:2015-05-13 18:39:09

标签: swift

编辑我的问题:

Grimxn,我创建了一个子类,可以看到它的工作原理,因为borderWidth和颜色。但是我仍然有一些关于如何添加我的功能的问题:

  1. 我应该编码" func textField(textField:UITextField"或" func textField(textField:MyCustomTextField"?
  2. 我该怎么做"如果textField == numberField01 {" ?
  3. 我如何称呼这个'从ViewController代码?

    class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var numberField01: UITextField!
    @IBOutlet weak var numberField02: MyCustomTextField!
    
    override func viewDidLoad() {
    super.viewDidLoad()
    numberField01.delegate = self
    numberField01.keyboardType = UIKeyboardType.NumberPad
    numberField02.delegate = self
    numberField02.keyboardType = UIKeyboardType.NumberPad
    

    }

    class MyCustomTextField: UITextField {
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layer.borderColor = UIColor.redColor().CGColor
        self.layer.borderWidth = 1.5
    
        func textField(textField: UITextField,
            shouldChangeCharactersInRange range: NSRange,
            replacementString string: String)
            -> Bool {
                var result = true
                var prospectiveText = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
                prospectiveText = prospectiveText.stringByReplacingOccurrencesOfString(".", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                if textField == numberField01 {
                    if count(string)>0 {
                        let disallowedCharacterSet = NSCharacterSet(charactersInString: "0123456789").invertedSet
                        let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
                        let resultingStringLengthIsLegal = count(prospectiveText) <= 4
                        let scanner = NSScanner(string: prospectiveText)
                        let resultingTextIsNumeric = scanner.scanDecimal(nil) && scanner.atEnd
                        result = replacementStringIsLegal && resultingStringLengthIsLegal && resultingTextIsNumeric
                    }
                }
                return result
        }
    }
    
    }
    

    原始问题: 以下代码适用于一个文本字段(numberField01)。它确保输入仅为十进制,放置小数点,并防止用户粘贴非十进制字符串。但我有更多的按钮......(numberField02及以上)。如何处理更多按钮,而不只是复制每个按钮的代码?

    class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var numberField01: UITextField!
    @IBOutlet weak var numberField02: UITextField!
    
    override func viewDidLoad() {
    super.viewDidLoad()
    numberField01.delegate = self
    numberField01.keyboardType = UIKeyboardType.NumberPad
    numberField02.delegate = self
    numberField02.keyboardType = UIKeyboardType.NumberPad
    }
    
    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    }
    
    // Tap background to add decimal point and defocus keyboard
    
    @IBAction func userTappedBackground(sender: AnyObject) {
    for view in self.view.subviews as! [UIView] {
      if let textField = view as? UITextField {
        if count(numberField01.text) > 0 {
            var numberString = numberField01.text
            numberString = numberString.stringByReplacingOccurrencesOfString(".", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
            var numberFromString = Double(numberString.toInt()!) / 100
            numberField01.text = String(format:"%.2f", numberFromString)
        }
        textField.resignFirstResponder()
      }
    }
    }
    
    func textField(textField: UITextField,
        shouldChangeCharactersInRange range: NSRange,
        replacementString string: String)
        -> Bool {
            var result = true
            var prospectiveText = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
            prospectiveText = prospectiveText.stringByReplacingOccurrencesOfString(".", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
            if textField == numberField01 {
                if count(string)>0 {
                    let disallowedCharacterSet = NSCharacterSet(charactersInString: "0123456789").invertedSet
                    let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
                     let resultingStringLengthIsLegal = count(prospectiveText) <= 4
                    let scanner = NSScanner(string: prospectiveText)
                    let resultingTextIsNumeric = scanner.scanDecimal(nil) && scanner.atEnd
                    result = replacementStringIsLegal && resultingStringLengthIsLegal && resultingTextIsNumeric
                }
            }
            return result
    }
    }
    

1 个答案:

答案 0 :(得分:1)

类似于以下内容:

    Button GetButtonCommonFeatures(Button myButton)
    {
        Write common code here....
        e.g. myButton.delegate = self;...
        return myButton;
    }

然后为每个按钮调用您的方法。让我们以numberField01为例。您将在适用于每个按钮的方法中包含代码。

    numberField01 = GetButtonCommonFeatures(numberField01);

希望这有帮助