我在一些textFields中输入了用户输入的数字。然后我从它们中做出一些组合,然后做一系列发现找出哪个数字大于另一个。例如,a和b中的a更大,然后c和d更大。然后从两个获胜者中获得更多。我希望我的最终答案显示在另一个viewController上的标签中,但我遇到了麻烦。 我在名为“PrisonerModel.swift”的swift文件中创建了一个名为“help”的结构,并希望将答案显示在MatrixViewController的答案标签中。我相信我的问题在调用答案的过程中会出现问题。
我的代码:
import Foundation
struct PrisonerModel {
var x: String
var y: String
var z: String
var a: String
var b: String
var c: String
var d: String
var e: String
var answer: String
}
struct help {
var f: String
func greaterInt(x: Int, _ y: Int) -> Int {
if x > y {
return x}
return y
}}
第二个ViewController
class MatrixViewController: UIViewController {
@IBOutlet weak var P1CoopCoop: UILabel!
@IBOutlet weak var P2CoopCoop: UILabel!
@IBOutlet weak var P1DefCoop: UILabel!
@IBOutlet weak var P2DefCoop: UILabel!
@IBOutlet weak var P1CoopDef: UILabel!
@IBOutlet weak var P2CoopDef: UILabel!
@IBOutlet weak var P1DefDef: UILabel!
@IBOutlet weak var P2DefDef: UILabel!
@IBOutlet weak var answer1: UILabel!
var labelText1 = ""
var model: PrisonerModel?
var answer: help?
override func viewDidLoad() {
super.viewDidLoad()
P1CoopCoop.text = model?.x
P2CoopCoop.text = model?.y
P1DefCoop.text = model?.z
P2DefCoop.text = model?.a
P1CoopDef.text = model?.b
P2CoopDef.text = model?.c
P1DefDef.text = model?.d
P2DefDef.text = model?.e
answer1.text = answer?.f
Segue公司
prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showMatrix" {
if let matrixVC = segue.destinationViewController as? MatrixViewController {
let pm = PrisonerModel(x: Text3.text ?? "", y: Text3.text ?? "", z: Text4.text ?? "", a: Text1.text ?? "", b: Text1.text ?? "", c: Text4.text ?? "", d: Text2.text ?? "", e: Text2.text ?? "", answer: String(Int(Text1.text! + Text3.text!)) ?? "")
matrixVC.model = pm
}
}
}
答案 0 :(得分:0)
不清楚"回答"应该是,但一些一般的建议是将您的文本值转换为整数,以便您可以对它们执行适当的数学运算。
我会实施func shouldPerformSegue(withIdentifier identifier: String,
sender: Any?) -> Bool
来首先验证您的输入并创建您的模型:
struct PrisonerModel {
var x: Int
var y: Int
var z: Int
var a: Int
var b: Int
var c: Int
var d: Int
var e: Int
var answer: Int
}
var model: PrisonerModel?
override func shouldPerformSegue(withIdentifier identifier: String,
sender: Any?) -> Bool {
if let identifier == "showMatrix" {
guard let a = Int(text1.text!),
let b = Int(text1.text!),
let c = Int(text4.text!),
let d = Int(text2.text!),
let e = Int(text2.text!),
let x = Int(text3.text!),
let y = Int(text3.text!),
let z = Int(text4.text!),
let answer = Int(text1.text + text3.txt) else {
// Probably set some text in a label informing the user that their input is invalid
return false
}
self.model = PrisonerModel(x:x, y:y, z:z, a:a, b:b, c:c, d:d, e:e, answer:answe)
return true
}
}
return false
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showMatrix" {
if let matrixVC = segue.destinationViewController as? MatrixViewController,
let model = self.model {
matrixVC.model = model
}
}
}
注意我已将文本字段属性的名称更改为以常规方式使用小写字母开头。如果您的模型属性的名称比a,b,c等更好,那么您的代码可能会更清晰
除非您已明确指定text
属性,否则使用nil
属性的强制解包是安全的。默认情况下,UITextField将为text
分配一个字符串。