2017-06-18 187 views
0

我將以一個事實說明這個問題,我嚴格是一個涉及xcode的設計師。我試圖建立一個'愚蠢的'原型。Swift 3 - 將數據從一個視圖傳遞到另一個視圖

我想設置2個VC。第一個VC有一個文本字段,允許用戶鍵入一個搜索參數,當點擊「返回」時,它應該將該參數傳遞給下一個VC。在這個特定的VC上,我還在導航欄中設置了「返回」按鈕。

我遇到的問題是我無法獲得第一個VC的後退按鈕的代碼,以便與傳遞變量的代碼一起使用。它會導致應用程序在任何時候碰到第一個視圖控制器上的後退按鈕時崩潰。 Xcode注意到問題出在代碼中傳遞變量的地方。這是兩個VC的代碼。

import UIKit 

class SearchViewController: UIViewController, UITextFieldDelegate { 

@IBOutlet weak var searchText: UITextField! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) 
    self.navigationController?.navigationBar.shadowImage = UIImage() 
    self.navigationController?.navigationBar.isTranslucent = true 
    searchText.delegate = self 
    searchText.becomeFirstResponder() 
    // Do any additional setup after loading the view. 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    var destinationController = segue.destination as! ResultsViewController 
    destinationController.searchItem = searchText.text! 
} 

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
    if searchText.text != "" { 
     performSegue(withIdentifier: "resultsSegue", sender: self) 
     textField.resignFirstResponder() 
     return false 
    } 

    // self.view.endEditing(true) 

    return true 
} 



override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

override func viewWillAppear(_ animated: Bool) 
{ 
    super.viewWillAppear(animated) 
    self.navigationItem.hidesBackButton = true 
} 



} 

結果視圖控制器的代碼是

import UIKit 

class ResultsViewController: UIViewController { 

@IBOutlet weak var userSearchInputLabel: UILabel! 
@IBAction func btnReturnToSearch(_ sender: Any) { 
    performSegue(withIdentifier: "segueBackToSearch", sender: self) 
} 


var searchItem = String() 



override func viewDidLoad() { 

    super.viewDidLoad() 
    userSearchInputLabel.text = searchItem 
    self.extendedLayoutIncludesOpaqueBars=true; 
    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

override func viewWillAppear(_ animated: Bool) 
{ 
    super.viewWillAppear(animated) 
    self.navigationItem.hidesBackButton = true 
} 

@IBAction func unwindToResults(segue:UIStoryboardSegue) { } 

} 

回答

5

如果您SearchViewControllerinitial VC那麼爲什麼你把back按鈕呢?你想要的地方Go Back to?我認爲它不應該在那裏。
此外,除非你真的需要做的事情編程在Xcode中,您應該只使用Storyboard做嵌入SearchViewControllerNavigationController(從菜單中選擇Controller則:Editor-> Embed in -> Navigation Controller)。

然後創建segueSearchViewControllercontroller icon on the topResultViewController,你可以爲segue集標識符像你已經知道(如你在代碼中使用它)。
在您的ResultViewController中,如果您已經完成了上述步驟,那麼您不需要button代替returnToSearch,您可以刪除@IBAction for btnReturnToSearch(_ sender: Any)。因爲您將獲得ResultViewController頂部的<Back按鈕作爲Navigation Item以免費返回到SearchVeiwController

***更新根據最新評論****
好的。我終於明白你的問題。在你的準備(爲:發件人)。你需要 。檢查Segue公司標識:

if segue.identifier == "CHANGE_ME_WITH_YOUR_SEGUE_FOR_RESULT_IDENTIFIER" { 
... 
} 

你傳下去的搜索結果之前。 ResultViewController。因爲當你退回時,你不會去ResultViewController,而是MainViewController

+0

SearchViewController不是初始視圖。搜索VC會連接到它之前的一個主要VC。我只是試圖將導航控制器嵌入到結果視圖控制器中。所以現在搜索和結果VC都有導航控制器。但是,當我嘗試將搜索VC中輸入的值傳遞給結果VC時,該應用仍然崩潰。當我註釋掉這些代碼行時,它可以正常工作(雖然沒有傳遞任何信息) –

+0

控制檯正在給我這些信息「無法將'UINavigationController'類型的值(0x1066124e8)轉換爲'Application11.ResultsViewController' –

+0

請提供您的項目的最小內容仍會崩潰。否則,將很難幫助您調試問題。 – Gray

0

其實我看到我做錯了。由於在我的搜索視圖控制器中有多個賽段,因此在執行將數據傳遞到結果視圖控制器的賽格之前,我需要對賽格標識名稱進行檢查。因爲我沒有這個檢查,導致應用程序崩潰。現在我只需要弄清楚如何阻止我的標籤欄導航消失...

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "resultsSegue" { 
    let destVC = segue.destination as! UINavigationController 
    let destinationController = destVC.topViewController as! ResultsViewController 
    destinationController.searchItem = searchText.text! 
    } 
} 
相關問題