2016-08-23 47 views
-2

我試圖值傳遞給第二個視圖控制器爲什麼我的數據沒有從表視圖傳遞給我的第二個視圖控制器?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "headToDetail" { 
      let indexPath = self.tableView.indexPathForSelectedRow!.row 
      print("SELECTED INDEX \(indexPath)") 
      let destVC = segue.destinationViewController as! SecondViewController 
      print("Segue TEst \(self.toPass)") 
      destVC.vieSegue = self.toPass 
     } 

我已經刪除didSelectRowAtIndexPath方法,只是要使用Segue公司,但是當我在一排電池接頭,我得到:

fatal error: unexpectedly found nil while unwrapping an Optional value 

這是對原始代碼的重大更新。

+2

爲什麼你的prepareForSegue是空的? –

+0

嘿,通常你有類似 - >重寫func prepareForSegue(segue:UIStoryboardSegue,sender:AnyObject?){ super.prepareForSegue(segue,sender:sender); something = Something(); 讓vc = segue.destinationViewController as! YourViewController; vc.setSomething(something); } –

回答

0

,因爲你創建一個新的對象時,它是空的......

var destination = SecondViewController() 

...傳遞您的數據......

destination.vieSegue = self.toPass! 

...然後讓它走出去的範圍沒有顯示它,保存它或做其他有用的事情。

1

的問題是,你在行

var destination = SecondViewController() 

創建一個新的ViewController但隨後的控制器只是重新分配,因爲沒有指向它(有它沒有提到它掉出來的範圍)。你還沒有進入它,也沒有進行任何繼續。

如何解決?基本上有兩種選擇。

故事板

如果你在故事板的工作,你必須創建第一ViewControllerSecondViewController之間的SEGUE。你必須給它一個identifier

然後在didSelectRowAtIndexPath你會寫

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    tableView.deselectRowAtIndexPath(indexPath, animated: true) 

    let row = indexPath.row 
    print(fruits[row]) 
    self.toPass = fruits[row] 

    performSegueWithIdentifier("YOUR_SEGUE_IDENTIFIER", sender: nil) 
} 

然後prepareForSegue你的屬性設置爲目的地SecondViewController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "YOUR_SEGUE_IDENTIFIER" { 
     let destVC = segue.destinationViewController as! SecondViewController 

     destVC.vieSegue = self.toPass! 
    } 
} 

手冊推

第二個選項是將didSelectRowAtIndexPath方法

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    tableView.deselectRowAtIndexPath(indexPath, animated: true) 

    let row = indexPath.row 
    print(fruits[row]) 

    let destination = SecondViewController() 
    destination.vieSegue = fruits[row] 

    // if you are using navigation controller 
    navigationController?.pushViewController(destination, animated: true) 

    // if you want to present it modally 
    // presentViewController(destination, animated: true, completion: nil) 
} 

第二個版本不強迫你創建塞格斯中手動推控制器。

+0

嗨大衛。 我跟着你的代碼,但它似乎並沒有工作。如果你把它放在故事板上,我認爲你不需要演奏塞克威爾的縮影。我仍然無法傳遞數據。奇怪的是我有打印語句,當我點擊表格行時。看起來像覆蓋func prepareForSegue運行之前didSelectRowAtIndex路徑 – BostonMacOSX

+0

嗯,這不應該發生。如果你的segue沒有連接到一個按鈕或類似的東西,那麼你需要執行'performSegueWithIdentifier'。如果它連接到這樣的東西,你可以改變它只連接在兩個控制器之間。無論如何,如果這不起作用,請嘗試第二種方法手動實例化'SecondViewController'。雖然如果你在故事板中有你的控制器,你將不得不使用'storyboard.instantiateViewControllerWithIdentifier'。 – frooby

+0

嗨大衛。你上面的代碼改變了嗎?我在網上讀到,你不應該同時擁有這兩種方法。它取決於你是否在tableview中將segue設置爲第二個控制器,或者將視圖控制器設置爲另一個viewcontroller。 – BostonMacOSX

相關問題