gpt4 book ai didi

ios - 多个选择器 View : unexpectedly found nil while unwrapping an Optional value

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

我正在尝试使用以下代码创建一个具有多个pickerviews的 View Controller :

import UIKit
import Firebase
import FirebaseAuth

class DormViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var dormName: UIPickerView!
@IBOutlet weak var entryway: UIPickerView!
@IBOutlet weak var roomNumber: UIPickerView!
@IBOutlet weak var nextButton: UIButton!

var ref: DatabaseReference!

let dorms = ["Adams", "Apley", "Cabot", "Canaday", "Currier", "DeWolfe 10", "Dewolfe 20", "Dunster", "Eliot", "Fairfax", "Grays", "Greenough", "Hollis", "Holworthy", "Hurlbut", "Inn at Harvard", "Kirkland", "Leverett Towers", "Leverett McKinlock Hall", "Lionel", "Lowell", "Massachusetts Hall", "Mather Lowrise", "Mather Tower", "Matthews", "Mower", "New Quincy", "Pennypacker", "Pforzheimer", "Pforzheimer Jordan North", "Pforzheimer Jordan South", "Stone Hall", "Stoughton", "Straus", "Thayer", "Weld", "Wigglesworth", "Winthrop" ]

let entrances = ["N/A", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

let rooms = ["N/A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99"]

override func viewDidLoad() {
super.viewDidLoad()

ref = Database.database().reference()

dormName = UIPickerView()
entryway = UIPickerView()
roomNumber = UIPickerView()

dormName.delegate = self
dormName.dataSource = self
entryway.delegate = self
entryway.dataSource = self
roomNumber.delegate = self
roomNumber.dataSource = self


dormName.tag = 0
entryway.tag = 1
roomNumber.tag = 2

// Do any additional setup after loading the view.
}

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

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 1 {
return dorms[row]
}
else if pickerView.tag == 2 {
return entrances[row]
}
else {
return rooms[row]
}
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 1 {
return dorms.count
}
else if pickerView.tag == 2 {
return entrances.count
}
else {
return rooms.count
}
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let user = Auth.auth().currentUser

if let user = user {
let uid = user.uid
var entry = entrances[row]
var num = rooms[row]

if entrances[row] == "N/A" {
entry = ""
}

if rooms[row] == "N/A" {
num = ""
}

self.ref.child("Dorms").child(uid).setValue(["Location": dorms[row] + " " + entry + "-" + num ])
}
else {
print("error")
}
}

@IBAction func nextButton(_ sender: Any) {
performSegue(withIdentifier: "tohomepage", sender: self)
}
}

但是,我收到错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

在我的 viewDidLoad() 函数中,当我调用电话时:

dormName.delegate = self

我尝试删除委托(delegate)和数据源调用,但仍然发生相同的错误。我还尝试删除标签并按名称引用选择器 View ,如下所示:

pickerview == "dormName"

但随后我收到索引超出范围错误。

我做错了什么?

最佳答案

当您从 xib 或 Storyboard 拖动 UIPickerView 时,您不需要调用一些 init :

dormName = UIPickerView()
entryway = UIPickerView()
roomNumber = UIPickerView()

删除它,然后重试

关于ios - 多个选择器 View : unexpectedly found nil while unwrapping an Optional value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44296918/

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