OGeek|极客世界-中国程序员成长平台

标题: ios - 从 UIpickerview 中提取值作为变量 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:25
标题: ios - 从 UIpickerview 中提取值作为变量

所以我有这个 UIpickerview 的代码,我想获得选择作为变量的值以供以后使用,我尝试使用可变重力但它说“从未使用过可变重力的初始化......”这是我的代码:

 var g = ["9.807", "3.711"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return g.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return g[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    var gravity = Float(g[row])
}



Best Answer-推荐答案


您需要在方法范围之外声明 var:

var gravity: Float?
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
   gravity = Float(g[row])
}

局部变量的作用域仅限于方法,当您不在方法中使用它时,您会收到警告。

这是 Swift 而不是 Javascript

关于ios - 从 UIpickerview 中提取值作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43235727/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4