尽管在 IB 中链接,但标签被发现为 nil
Label is found as nil despite being linked in IB
Swift3/Xcode 8.1
以下代码...
class PageContentVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate{
var array = [String]()
var currentIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
array = ["1","2","3","4"]
self.dataSource = self
self.delegate = self
self.setViewControllers([helperFunction(index: currentIndex)], direction: .forward, animated: true, completion: nil)
}
func helperFunction(index: Int) -> UIViewController{
let newVC = storyboard?.instantiateViewController(withIdentifier: "PageContent") as! PageContent
print("currentIndex is: \(currentIndex)")
print("value at currentIndex is: \(array[currentIndex])")
newVC.PageContentLabel.text = array[currentIndex]
return newVC
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
if (currentIndex == 0) || (currentIndex == NSNotFound) {
return nil
}
currentIndex -= 1
return helperFunction(index: currentIndex)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
if (currentIndex == array.count - 1) || (currentIndex == NSNotFound) {
return nil
}
currentIndex += 1
return helperFunction(index: currentIndex)
}
}
...使应用程序崩溃并在以下行出现 "Unexpectedly found nil whilst unwrapping an optional value" 错误:
newVC.PageContentLabel.text = array[currentIndex]
'PageContentLabel' 肯定是通过界面生成器链接的,当我注释掉有问题的行时,应用程序运行正常。
我在这里错过了什么?
标签是 nil
,因为您已经实例化了视图控制器,但是故事板中定义的视图层次结构(如 IB 中的配置)在显示相应的视图之前不会构建。您应该在视图控制器中添加一个字符串 属性 并填充 helperFunction
,而不是 IBOutlet
。然后,PageContent
的 viewDidLoad
应该使用该字符串 属性 并使用它来设置 UILabel
的 text
。最重要的是,在调用 viewDidLoad
之前不要尝试使用插座。
使用outlets不是更简单吗?
@IBOutlet weak var PageContentLabel: UILabel!
ctrl 从情节提要中的 UILabel 拖动到 class 正下方的 PageContentVC 文件,然后创建一个出口并根据需要命名。
Swift3/Xcode 8.1
以下代码...
class PageContentVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate{
var array = [String]()
var currentIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
array = ["1","2","3","4"]
self.dataSource = self
self.delegate = self
self.setViewControllers([helperFunction(index: currentIndex)], direction: .forward, animated: true, completion: nil)
}
func helperFunction(index: Int) -> UIViewController{
let newVC = storyboard?.instantiateViewController(withIdentifier: "PageContent") as! PageContent
print("currentIndex is: \(currentIndex)")
print("value at currentIndex is: \(array[currentIndex])")
newVC.PageContentLabel.text = array[currentIndex]
return newVC
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
if (currentIndex == 0) || (currentIndex == NSNotFound) {
return nil
}
currentIndex -= 1
return helperFunction(index: currentIndex)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
if (currentIndex == array.count - 1) || (currentIndex == NSNotFound) {
return nil
}
currentIndex += 1
return helperFunction(index: currentIndex)
}
}
...使应用程序崩溃并在以下行出现 "Unexpectedly found nil whilst unwrapping an optional value" 错误:
newVC.PageContentLabel.text = array[currentIndex]
'PageContentLabel' 肯定是通过界面生成器链接的,当我注释掉有问题的行时,应用程序运行正常。
我在这里错过了什么?
标签是 nil
,因为您已经实例化了视图控制器,但是故事板中定义的视图层次结构(如 IB 中的配置)在显示相应的视图之前不会构建。您应该在视图控制器中添加一个字符串 属性 并填充 helperFunction
,而不是 IBOutlet
。然后,PageContent
的 viewDidLoad
应该使用该字符串 属性 并使用它来设置 UILabel
的 text
。最重要的是,在调用 viewDidLoad
之前不要尝试使用插座。
使用outlets不是更简单吗?
@IBOutlet weak var PageContentLabel: UILabel!
ctrl 从情节提要中的 UILabel 拖动到 class 正下方的 PageContentVC 文件,然后创建一个出口并根据需要命名。