gpt4 book ai didi

swift - 避免为很多按钮复制代码

转载 作者:行者123 更新时间:2023-11-28 07:06:52 24 4
gpt4 key购买 nike

编辑我的问题:

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

  1. 我应该编码“func textField(textField: UITextField”还是“func textField(textField: MyCustomTextField”)?
  2. 我应该如何处理“if 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
    }
    }

最佳答案

所以类似于下面的内容:

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

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

    numberField01 = GetButtonCommonFeatures(numberField01);

希望对你有帮助

关于swift - 避免为很多按钮复制代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30222805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com