更新时间:2023-02-02 13:46:34
好的,使用评论中的信息 - 特别是这个(重点是我的):
Ok, with the information from the comments - specifically this (emphasis mine):
@Losiowaty 我明白你的意思,在我的StoryBoard 并连接它,但为什么我有 PickerView 即使 我没有在我的 StoryBoard 上放一个 UIPicerView
@Losiowaty I see what you mean, to create a UIPickerView on my StoryBoard and connect it, but why I have a PickerView even if I don't put a UIPicerView on my StoryBoard
选择器视图的 @IBOutlet
具有误导性.根据以上信息,问题似乎是您从未分配给 self.pickerView
.在 viewDidLoad
你有这个代码:
The @IBOutlet
for the picker view was misleading. With the above information, it seems that the issue is that you never assign to self.pickerView
. In viewDidLoad
you have this code :
let pickerView = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 100))
pickerView.showsSelectionIndicator = true
pickerView.delegate = self
pickerView.dataSource = self
只需在下面添加 self.pickerView = pickerView
就可以了.
Simply add self.pickerView = pickerView
below this and everything should be fine.
请注意,当您以编程方式创建视图时,您不需要也不应该将它们注释为 @IBOutlet
s.它降低了代码对其他人的可读性,甚至可能在一段时间后对您来说也是如此.IBOutlet
代表 Interface Builder Outlet.
Note, that when you create views programatically you don't need and shouldn't annotate them as @IBOutlet
s. It makes the code less readable for someone else, and maybe even for you after some time. IBOutlet
stands for Interface Builder Outlet.