2017-02-10 57 views
0

帶有UICollectionView的UIViewController工作正常,如果我從故事板中啓動它。然而,當我嘗試推出這樣的:如何在Swift中顯示帶有UICollectionView的UIViewController?

let imagePicker = imagePickerControllerViewController() 
imagePicker.modalPresentationStyle = .OverCurrentContext 
presentViewController(imagePicker, animated: true, completion: nil) 

它崩潰上線:

collectionView.dataSource = self 

的問題是,如何展現一個UIViewController(具有UICollectionView)模態正常嗎?

+0

你能後的錯誤訊息? –

+0

致命錯誤:在解包可選值時意外發現爲零 –

+0

這意味着在初始化collectionView或數據源之前調用collectionView.dataSource = self。 –

回答

0

替換:

let imagePicker = imagePickerControllerViewController() 

有了:

let imagePicker = storyboard.instantiateViewControllerWithIdentifier("imagePicker") as! imagePickerControllerViewController 
相關問題