PresentViewController 在 instantiateViewController 上发现 nil 值
PresentViewController found nil value on instantiateViewController
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView.tag == 2{
switch indexPath.item {
case 0:
if let vc = storyboard?.instantiateViewController(withIdentifier: "offersScreen") as? OffersVC {
vc.cityID = self.cityID!
vc.categoryID = indexPath.item + 1
vc.headingLBL.text = self.names[indexPath.item]
vc.cityLBL.text = self.selectCity.text!
present(vc, animated: true, completion: nil)
}
case 1:
print("You're heading VC 1!")
case 2:
print("You're heading VC 2!")
case 3:
print("You're heading VC 3!")
default:
print("Something went wrong")
}
}
}
enter image description here
我写的一切都很完美,我知道我犯了什么错误。它发现 on present 没有价值。我从标签栏 viewController 中显示 viewcontroller 有 4 个值
请帮助或指导。
提前致谢。
原因是在 vc 加载
之前所有插座都为零
选项#1
vc.loadViewIfNeeded()
vc.headingLBL.text = self.names[indexPath.item]
选项#2
为您需要发送的内容制作变量
class OffersVC:UIViewController {
var sendedStr = ""
然后里面viewDidLoad
self.headingLBL = sendedStr
查看您的图像描述,我认为问题在于您试图在未先初始化的情况下展开 'headingLBL'。
见
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView.tag == 2{
switch indexPath.item {
case 0:
if let vc = storyboard?.instantiateViewController(withIdentifier: "offersScreen") as? OffersVC {
vc.cityID = self.cityID!
vc.categoryID = indexPath.item + 1
vc.headingLBL.text = self.names[indexPath.item]
vc.cityLBL.text = self.selectCity.text!
present(vc, animated: true, completion: nil)
}
case 1:
print("You're heading VC 1!")
case 2:
print("You're heading VC 2!")
case 3:
print("You're heading VC 3!")
default:
print("Something went wrong")
}
}
}
enter image description here
我写的一切都很完美,我知道我犯了什么错误。它发现 on present 没有价值。我从标签栏 viewController 中显示 viewcontroller 有 4 个值 请帮助或指导。 提前致谢。
原因是在 vc 加载
之前所有插座都为零选项#1
vc.loadViewIfNeeded()
vc.headingLBL.text = self.names[indexPath.item]
选项#2
为您需要发送的内容制作变量
class OffersVC:UIViewController {
var sendedStr = ""
然后里面viewDidLoad
self.headingLBL = sendedStr
查看您的图像描述,我认为问题在于您试图在未先初始化的情况下展开 'headingLBL'。
见