2016-05-13 26 views
1

我正在處理一個具有搜索功能的項目。我有3個視圖控制器,即HomeViewController,SearchResultViewController和DetailViewController。如何執行SegueWithIdentifier()從動態創建的UIViewController,其故事板屬性是無

在主頁面上,我在導航欄中有一個搜索欄(使用UISearchController)以及一些虛擬內容。這裏重要的是我使用單獨的控制器(在我的情況下它是SearchResultController)來顯示結果。這個SearchResultController實際上是TableViewController的子類,它在tableview中顯示結果。一切都很好,直到這裏。

問題

現在,當我從結果中選擇任何行(即SearchResultViewController)我想打開另一個視圖控制器(DetailViewController),我會展示這個選項的細節。這是一件非常簡單的事情,但我被困在這裏。

好,與我實現didSelectRowAtIndexPath方法方法,並從我打電話performSegueWithIdentifier,我得到錯誤「接收器與標識沒有賽格瑞」,因爲故事板屬性是零(據中提到的問題不斷在文檔中)。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     print("storyboard property is \(self.storyboard)") 
     performSegueWithIdentifier(segueIdentifier, sender: indexPath.row) 
    } 

注: segueIdentifier是同它的故事板。 我得到的故事板屬性爲零。

我猜

當我在搜索欄上點擊,iOS版在內部創建並加載SearchResultController的實例,以顯示的結果,因此它的Storyboard屬性是零,因爲這一點,我發現了以上錯誤。

有人可以解決這個問題或任何指向正確的方向。

+1

你不需要在這種情況下,賽格瑞,只是實例化視圖控制器和目前它 – Scriptable

+0

你不能Segue公司這故事板都沒有定義創建實例,並從導航只是推或呈現 –

+0

@Scriptable是的,只是使用presentViewController(),它工作正常。謝謝 –

回答

2

如果要創建視圖 - 控制程序,那麼你應該在推導航控制器上一樣,

UIViewController *vc = [[UIViewController alloc]init]; //your viewcontroller here 

[self.navigationController pushViewController:vc animated:YES]; 

//or if you want to present it then 

[self presentViewController:vc animated:YES completion:nil]; 

因爲沒有迅速

故事板

像這樣的事情沒有賽格瑞您不能執行SEGUE

let vc: UIViewController = UIViewController() 
    //your viewcontroller here 
    self.navigationController!.pushViewController(vc, animated: true) 
    //or if you want to present it then 
    self.presentViewController(vc, animated: true, completion: { _ in }) 

避免任何錯誤!

希望這將有助於:)

相關問題