1

Swift 2. 我正在尝试使用 label.text 的答案!再次繁殖。标签是可选的,但必须与 Double 相乘。

@IBOutlet weak var testLabel: UILabel!
@IBOutlet weak var first: UITextField!
@IBOutlet weak var second: UITextField!

func calculation1() {
let dfirst = Double(first.text!)
let dsecond = Double(second.text!)
if dfirst != nil && dsecond != nil {
let answerCal = ceil(dfirst! * dsecond!)
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .DecimalStyle
testLabel.text = numberFormatter.stringFromNumber(answerCal)
}
}

任何帮助将不胜感激。

4

1 回答 1

0

首先,很抱歉,因为我已将其放在平板电脑上,这意味着我无法访问代码编辑器。但是,这是我对您的问题的看法:

@IBOutlet weak var testLabel: UILabel!
@IBOutlet weak var first: UITextField!
@IBOutlet weak var second: UITextField!

func calculation1() -> Void {
    // Check our two text fields contain text
    guard let _ = first.text,
          let _ = second.text else { return }
  c // Check that the text can be parsed as Doubles
  c guard let dfirst : Double = Double(first.text!),
    let dsecond : Double = Double(second.text!) else { return }

    // Now that we know we have two valid numbers, do the calculation
    let answerCal : Double = ceil(dfirst! * dsecond!)

    // Fetch a number formatter
    let numberFormatter : NSNumberFormatter = {
        let nF : NSNumberFormatter = NSNumberFormatter()
        nF.numberStyle = .DecimalStyle
        return nF
    }()
    // And update the text field with the answer if it produces a valid result
    if let answerText : String =     numberFormatter.stringFromNumber(answerCal) {
         testLabel.text = answerText
    }
}

您必须小心 UI 元素,如果您不先检查,用户理论上可以输入任何内容并导致您的应用程序崩溃。因此,如果文本字段中没有文本或这些字段包含不可用的文本,则将安全退出方法调用的保护语句。

您从数字格式化程序中获得 Optional 的原因是因为无法保证格式化程序能够为您提供有效的字符串。因此,您会看到,在尝试更新文本字段之前,我已经明确检查了 numbe 格式化程序是否会生成非零结果。

(注意 - 我很欣赏数字格式化程序的关闭在技术上超出了要求,但我发现有时在配置对象时更清楚。)

更新:

Vidal - 你试过我发给你的代码吗?我很快就根据我昨晚整理的内容整理了以下内容。这显然很粗糙,但表明我建议的方法效果很好,除非我遗漏了什么。我能想到的唯一问题是您在界面构建器中做了一些我不知道的事情。希望有帮助!

import UIKit

class ViewController: UIViewController {

    var calculatorView : UIView!
    var textFieldOne : UITextField!
    var textFieldTwo : UITextField!
    var calButton : UIButton!
    var answer : UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.calculatorView = {
                let cV : UIView = UIView(frame: CGRect(origin: CGPointZero, size: CGSizeMake(400, 400)))
            cV.backgroundColor = UIColor.redColor()

            func buildTextField() -> UITextField {
                let tF : UITextField = UITextField()
                tF.backgroundColor = UIColor.whiteColor()
                tF.textColor = UIColor.blackColor()
                tF.placeholder = "Please enter number"
                return tF
            }

            self.textFieldOne = buildTextField()
            self.textFieldTwo = buildTextField()
            self.textFieldOne.frame = CGRect(origin: CGPointMake(20, 20), size: CGSizeMake(160, 50))
            self.textFieldTwo.frame = CGRect(origin: CGPointMake(220, 20), size: CGSizeMake(160, 50))

            self.calButton = {
                let cB : UIButton = UIButton()
                cB.setTitle("Calculate", forState: .Normal)
                cB.frame = CGRect(origin: CGPointMake(20, 80), size: CGSizeMake(360, 50))
                cB.addTarget(self, action: #selector(self.getAnswer), forControlEvents: UIControlEvents.TouchUpInside)
                return cB
            }()

            self.answer = {
                let a : UILabel = UILabel()
                a.backgroundColor = UIColor.blueColor()
                a.textColor = UIColor.whiteColor()
                a.frame = CGRect(origin: CGPointMake(20, 140), size: CGSizeMake(360, 50))
                return a
            }()

            cV.addSubview(textFieldOne)
            cV.addSubview(textFieldTwo)
            cV.addSubview(calButton)
            cV.addSubview(answer)

            return cV
        }()

        self.view.addSubview(calculatorView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func getAnswer() -> Void {
        // Check our two text fields contain text
        guard let _ = self.textFieldOne.text,
              let _ = self.textFieldTwo.text else { return }
        // Check that the text can be parsed as Doubles
        guard let dfirst : Double = Double(self.textFieldOne.text!),
              let dsecond : Double = Double(self.textFieldTwo.text!) else { return }

        // Now that we know we have two valid numbers, do the calculation
        let answerCal : Double = ceil(dfirst * dsecond)

        // Fetch a number formatter
        let numberFormatter : NSNumberFormatter = {
            let nF : NSNumberFormatter = NSNumberFormatter()
            nF.numberStyle = .DecimalStyle
            return nF
        }()
        // And update the text field with the answer if it produces a valid result
        if let answerText : String = numberFormatter.stringFromNumber(answerCal) {
            self.answer.text = answerText
        }
    }
}
于 2016-09-10T21:42:13.247 回答