2016-02-03 73 views
3

問題:在搜索結果斯威夫特搜索結果控制器Segue公司到另一個視圖控制器

我有一個表,視圖,用戶可以通過滾動找到的東西,或者使用搜索欄。搜索欄不是使用StoryBoard創建的。我的看法有一個UISearchController處理搜索欄和搜索結果更新。我遇到的問題是,因爲我的SearchResultsController由另一個控制器實例化,所以我無法對另一個視圖執行segue,或者至少我找不到解決方案。除了我的搜索結果控制器和其目的地視圖之間的一切之外,一切都可以正常工作。

我想這樣做

有我MyNavTableViewController委託MySearchResultsController。在搜索結果控制器中,當用戶點擊TableViewCell時,它將繼續到另一個視圖。我沒有使用StoryBoard來完成這個我很難使用segues轉換到另一個視圖。

如果我不能得到這個工作,我可能會做:

這是至關重要的我通過視圖之間的信息,對於我來說,我使用塞格斯始終做到了。但是,如果這不起作用,我可能會嘗試通過將其推送到導航控制器堆棧並將數據寫入共享數據庫或其他東西來模式地呈現視圖。我寧願使用segue。

研究: 絕對不止這些,但我不會在網址上佔用太多空間。

Creating a segue programmatically

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html

我的設置

我會盡力保持這個儘可能簡明扼要。有比我顯示更多的代碼。我只是試着把它清理乾淨,這樣我才能提出重要的東西。我也在改變一些名字,因爲可能有敏感信息。這可能不是什麼大不了的事情,但我寧願安全。

class MyNavTableViewController: UIViewController, UITableViewDataSource{ 

    //this is 
    @IBOutlet weak var tableView: UITableView! 
    var searchController: UISearchController! 


    override func viewDidLoad(){ 
     ...code 

     tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: tblId) 

    let resultsController = MySearchResultsController() 
    resultsController.databaseFilePath = databaseFilePath() 
    //this is essential that I use a segue because between my views I'm passing information between them. 
    resultsController.photo = photo! 

    searchController = UISearchController(searchResultsController:  resultsController) 
    let searchBar = searchController.searchBar 
    searchBar.placeholder = searchBarPlaceHolderText 
    searchBar.sizeToFit() 
    tableView.tableHeaderView = searchBar 
    searchController.searchResultsUpdater = resultsController 

    } 


} 


MySearchResultsController: UITableViewController, UISearchResultsUpdating { 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ 
    //self.performSegueWithIdentifier(imagePrepareStoryBoardId, sender: tableView.cellForRowAtIndexPath(indexPath)) 
    /*let imagePrepare = ImagePrepareController() 
    customSegue = SearchResultSegue(identifier: imagePrepareId, source: self, destination: imagePrepare)*/ 

    //neither storyboard nor navigationController can be nil. 
    let destVC = self.storyboard!.instantiateViewControllerWithIdentifier(imagePrepareStoryBoardId) 
    //destVC.photo = photo! 


    //self.presentViewController(destVC, animated: false, completion: nil) 

    self.navigationController!.pushViewController(destVC, animated: false) 


    } 
} 

我失敗的嘗試

1)直線上升賽格瑞 - 不工作,因爲MySearchResultsController是不是在故事板視圖。從我讀過的所有內容來看,segues只能在SB中創建。

2)將視圖推入導航堆棧。唯一的問題是,我不能在視圖之間發送數據(或者至少從我讀過的內容)。我也得到這個錯誤在這個突破點:

let destVC = self.storyboard!.instantiateViewControllerWithIdentifier(imagePrepareStoryBoardId) 

fatal error: unexpectedly found nil while unwrapping an Optional value 

我double檢查imagePrepareStoryBoardId。這是正確的。

3)使用自定義segue - 正如您從註釋掉的行中可以看出,這也不起作用。我一直在使用SB,因此這種方法對我來說有點新鮮。我可能會把它搞亂。

+0

爲什麼不使用協議作爲代表? –

+0

@ViníciusAlbino我不知道這是如何幫助的(從我自己的無知)。我的意思不是粗魯的。您可能會看到解決方案,並且可以輕鬆地將其總結爲此,但我沒有遵循。如果你能擴大這一點,我將不勝感激。 – enigmasck

回答

6

首先創建一個協議

protocol SelectedCellProtocol { 
    func didSelectedCell(text: String) 
} 
您UITableViewClass

聲明它

class MySearchResultsController: UITableViewController, UISearchResultsUpdating { 
    var delegate:SelectedCellProtocol? 
} 

,並在選定單元格的方法調用它:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ 
    self.delegate?.didSelectedCell(cell.textLabel?.text) 
} 

當你宣佈你的結果控制器,設置代表

let resultsController = MySearchResultsController() 
    resultsController.databaseFilePath = databaseFilePath() 
    //this is essential that I use a segue because between my views I'm passing information between them. 
    resultsController.photo = photo! 
resultsController.delegate = self 

    searchController = UISearchController(searchResultsController:  resultsController) 
    let searchBar = searchController.searchBar 
    searchBar.placeholder = searchBarPlaceHolderText 
    searchBar.sizeToFit() 
    tableView.tableHeaderView = searchBar 
    searchController.searchResultsUpdater = resultsController 

,然後實現協議

class MyNavTableViewController: UIViewController, UITableViewDataSource, SelectedCellProtocol{ 
    func didSelectedCell(text: String) { 
     print(text) 
     //make your custom segue 
    } 
} 
+0

工作就像一個魅力!儘管這很簡單,但我仍然認爲它很聰明。也許我太習慣於在框架中找到解決方案。謝謝! – enigmasck

相關問題