2017-03-26 193 views
0

準備點擊在TableView中一accessoryButton後,它會先功能,然後重定向我PrepareforSegueaccessoryButtonTappedForRowWith功能這在我的代碼中創建錯誤。問題與Segue公司和AccessoryButtonTappedForRowWith

請看看到我的代碼:

var indexPathAccessory: Int? 

override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) { 
    indexPathAccessory = indexPath.row 
} 

indexPathAccessory包含其中Accessorybutton被點擊的行的值。 第二個功能是:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "Add" { 
     let nc = segue.destination as! UINavigatorController 
     controller = nc.topViewController as! AVC 
     //Comment 1 
     controller.name = span[indexPathAccessory] 
     //Comment 2 

     controller.delegate = self 
    } 
} 

我點擊accessorybutton後得到錯誤的原因,它是功能,然後再重定向我prepareforSegueaccessoryButtonTappedForRowWith和因值indexPathAccessory哪還沒有,我得到這個錯誤

,纔有可能第一個跳accessoryButtonTappedForRowWith得到的indexPathAccessory然後prepareForSegue價值? 如果是,問題將被解決。

我嘗試添加一個測試時要返回indexPathAccessory爲零。沒有錯誤,但沒有以適當的方式 應用程序運行:
我可以看到controller.name等於「測試」,但是當我在AVC視圖控制器,成爲

有沒有建議嗎?

回答

0

最後它的工作通過刪除accessoryButtonTappedForRowWith功能,並添加以下代碼prepareforSegue功能:

 if let indexPath = tableView.indexPath(for: sender as! UITableViewCell) { 

      controller.beamName = "Test" 

      indexSpanAccessory = indexPath.row 
     } 
0

其中一個解決方案可以是 - 從故事板中刪除導致控件脫離手的繼續,並在您的accessoryButtonTappedForRowWith方法中寫入performSegue。

+0

我以前試過,但我沒有成功,如何發送的名稱和委託給下一個視圖控制器! –