訪問的ViewController部件我有2簡單視圖如下如何從PageViewController
第一個是網頁瀏覽第二個是簡單的viewController在頂部
的viewController碼的圖像視圖如下
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
imgView.image = UIImage(named: "dog0")
}
}
當我運行這個視圖控制器作爲初始視圖控制器。它工作正常。它顯示的圖像正常
但是當我做從頁面視圖控制器相同的用下面的代碼
class PageViewController: UIPageViewController, UIPageViewControllerDataSource {
let imageNames = ["dog0","dog1","dog2"]
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
let eachView = ViewController()
eachView.imageName = imageNames.first
let viewControllers = [eachView]
setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
return nil
}
}
和
class ViewController: UIViewController {
var imageName : String? {
didSet {
imgView.image = UIImage(named: imageName!)
}
}
@IBOutlet weak var imgView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
}
}
我得到以下錯誤
fatal error: unexpectedly found nil while unwrapping an Optional value
line
imgView.image = UIImage(named: imageName!)
什麼問題&如何解決這個問題?
我該如何解決這個問題? – LynAs
@LynAs我想我現在已經提供了足夠的解釋!如果您還有其他問題,請告訴我。 – matt
我正在實例化視圖控制器這樣'let eachView = self.storyboard?.instantiateViewController(withIdentifier:「imageScroller」)as! ViewController'仍然收到相同的錯誤。你能告訴我爲什麼嗎?? – LynAs