2015-05-13 46 views
1

編輯我的問題:避免複製很多按鈕的代碼

Grimxn,我做了一個子類,可以看到它的工作原因,因爲borderWidth和color。但我仍然對如何添加我的函數幾個問題:

  1. 我應該代碼「FUNC文本框(文本框:的UITextField」或「FUNC文本框(文本框:MyCustomTextField」?
  2. 我應該怎麼跟做「如果文本框== numberField01 {」?
  3. 如何從視圖控制器代碼 '稱之爲'?

    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: "").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: "").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

爲什麼不創建一個返回具有所有常用功能按鈕的方法? – Maxqueue

+0

你能解釋一下嗎,Maxqueue?或給我一個線索,我可以找到一個例子?謝謝! – arakweker

回答

1

因此,一些類似以下內容:

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

然後調用你的方法爲每個按鈕。例如,讓我們以numberField01爲例。您將在適用於每個按鈕的方法中包含代碼。

numberField01 = GetButtonCommonFeatures(numberField01); 

希望這有助於

+0

謝謝...給我一些時間玩... – arakweker

+0

或者你可以繼承UITextField ... – Grimxn

+0

謝謝格里姆斯。你可以給我一個例子嗎?與上述解決方案相比,有哪些優勢/劣勢?謝謝! – arakweker