簡體   English   中英

UITableViewCell故事板注冊取決於初始視圖控制器?

[英]UITableViewCell Storyboard Registration Dependent on Initial View Controller?

在我當前的XCode項目中,我有兩個視圖控制器。 第一個是初始視圖控制器,只有一個按鈕。 第二個視圖控制器是UITableViewController的子類。 我在故事板中定義了一個自定義表視圖單元,給它一個重用標識符,並定義了一個自定義類並連接了相關的IBOutlets。

啟動應用程序后,按下按鈕時,第二個視圖控制器通過self.presentViewController(secondViewController, animated: true, completion: nil)以模態方式顯示在第一個視圖控制器之上。 兩個視圖控制器未通過segue連接在故事板中。 在此模式搜索之后,在構建表的過程中,它崩潰並顯示錯誤:

'unable to dequeue a cell with identifier PlaylistCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

但是,當表視圖控制器是初始視圖控制器時,它不會崩潰並正確顯示所有數據。 我想知道nib注冊是否依賴於通過故事板連接到初始視圖控制器的視圖控制器?

編輯:我弄清楚了我的錯誤。 對於其他發現此問題的人,我正在初始化第二個視圖控制器,例如:

let vc = SecondViewController()

但是,如果通過Storyboard設計它,則必須像這樣初始化它:

let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController")

如您所知,您必須通過在情節提要中進行識別來實例化表格視圖控制器。

如果第二個控制器的情節提要與第一個控制器的情節提要相同,則也可以執行以下操作:

if let storyboard = self.storyboard, controller = storyboard.instantiateViewControllerWithIdentifier("Second") as? UITableViewController {
    self.presentViewController(controller, animated: true, completion: nil)
}

這樣您就不必對故事板的名稱進行硬編碼,並且可以確保故事板和視圖控制器不是零並且可以呈現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM