gpt4 book ai didi

arrays - 如何将对象从 pickerView append 到数组

转载 作者:行者123 更新时间:2023-11-30 10:43:04 25 4
gpt4 key购买 nike

我有一个在 pickerView 中使用的类对象数组。我需要将该对象 append 到另一个通过 UIButton 支持我的 tableView 的数组。我无法弄清楚如何获取 pickerViews Index,我得到的最接近的是错误“无法将类型'Int'的值转换为预期参数类型'Formula'”,不知道我做错了什么,并在此处询问之前搜索了几天。预先感谢您。

class Formula {
var formulaNumber: String = ""
var formulaTitle: String = ""
var base: String = ""
var baseValue: Double = 0.0
}



class AllFormula {

var arr: [Formula] = []
static var newArr : [Formula] = []


init() {

let test1 = Formula()
test1.formulaNumber = "test1"
test1.formulaTitle = "test1"
test1.base = "test1"
test1.baseValue = 10

let test2 = Formula()
test2.formulaNumber = "test2"
test2.formulaTitle = "test2"
test2.base = "test2"
test2.baseValue = 20

arr = [test1, test2]
AllFormula.newArr = []
}

}


class AddViewControllerTest : UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {


let allFormula = AllFormula()
let numberOfBatches = (1...100).map { String($0) }
@IBOutlet weak var picker: UIPickerView!

override func viewDidLoad() {
super.viewDidLoad()
picker.dataSource = self
picker.delegate = self

}



func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return allFormula.arr.count
}else{
return numberOfBatches.count
}

}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return allFormula.arr[row].formulaNumber

}
return numberOfBatches[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let chosenRun = allFormula.arr[row]
let Batches = self.numberOfBatches[row]

//these print the correct formulas and number of batches
print(chosenRun)
print(Batches)

}


@IBAction func AddRunTapped(_ sender: Any) {
//trying to get the object to pass into new array.
AllFormula.newArr.append(picker.selectedRow(inComponent: 0))

//also tried this, which works if it were inside the didSelectRow for the picker view. but i need it on a IBAction.
AllFormula.newArr.append(allFormulas.arr[row])

// where as if i choose the exact index it works.
AllFormula.newArr.append(allFormulas.arr[1])
}
}

最佳答案

nvm ...明白了。

在顶层声明变量。

var test = Formula()

did选择行

test = allFormula.arr[row]

IBA行动

AllFormula.newArr.append(test)

关于arrays - 如何将对象从 pickerView append 到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56383974/

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