編輯我的問題:避免複製很多按鈕的代碼
Grimxn,我做了一個子類,可以看到它的工作原因,因爲borderWidth和color。但我仍然對如何添加我的函數幾個問題:
- 我應該代碼「FUNC文本框(文本框:的UITextField」或「FUNC文本框(文本框:MyCustomTextField」?
- 我應該怎麼跟做「如果文本框== numberField01 {」?
如何從視圖控制器代碼 '稱之爲'?
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 } }
爲什麼不創建一個返回具有所有常用功能按鈕的方法? – Maxqueue
你能解釋一下嗎,Maxqueue?或給我一個線索,我可以找到一個例子?謝謝! – arakweker