1. 程式人生 > >Swift 關聯Storyboard中的controller解決UICollectionView must be initialized with a non-nil layout parameter

Swift 關聯Storyboard中的controller解決UICollectionView must be initialized with a non-nil layout parameter

在開發中會遇到已經寫好的Storyboard中需要新增新的功能的時候,需要將Storyboard中的Controller例項化後才能進一步地操作,不然會出現類似'UICollectionView must be initialized with a non-nil layout parameter'這種類似的錯誤,原因是工程中的類要呼叫Storyboard中的Controller,但是Storyboard中的Controller卻沒有找到對應的類。因此解決的辦法如下:

1.建立Storyboard中Controller對應的類物件,並將Controller和類關聯,然後設定id(我的CollectionViewController放在TabbarController中)

2.如果要呼叫則用如下程式碼:

<span style="white-space:pre">	</span>var photoView = PictrueTabBarController()
        photoView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("tabbar") 
<span style="white-space:pre">	</span>as! PictrueTabBarController