2016-09-16 38 views
-2

我是iOS開發的新手。我想問的一件事,當我點擊tableViewCell我想移動到新的viewController,但當我點擊它給我注意。點擊tableview單元不能移動到新的控制器

這裏是我的代碼 -

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    let row = indexPath.row 
    if row == 4 
    { 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let secondViewController = storyboard.instantiateViewControllerWithIdentifier("contact") as! contactsandlists 
     navigationController?.pushViewController(secondViewController, animated: true) 
     print("error") 
    } 

請解決我的問題

+0

錯誤是否打印? –

+0

是錯誤被打印,但它不會移動到進一步的視圖控制器 – Bansal

+1

在故事板,你需要嵌入你的當前控制器與NavigationController –

回答

0

嘗試使用此演示文稿。

這將呈現你是控制器,但它將沒有導航欄。

self.presentViewController(secondViewController, animated: true, completion: nil) 

如果您想與導航欄一起出示。

self.navigationController.pushViewController(secondViewController, animated: true) 
0

你有導航控制器嗎?如果你不這樣做,什麼事情都不會發生,你打印出「錯誤」。另一種方法是更改​​在AppDelegate中

let appDelegate = UIApplication.sharedApplication().delegate 

    if let window = appDelegate?.window { 
     window?.rootViewController = secondViewController 
     window?.makeKeyAndVisible() 
    } 

另一種方法是使用創建tableviewcell和其他視圖控制器之間的賽格瑞和使用performSegueWithIdentifier找到窗口屬性的根視圖控制器。

performSegueWithIdentifier("segueId", sender: nil) 
+0

可以解釋執行segue鑑定方法 – Bansal

+0

如何不同單元格的tableview將工作? – Bansal

+0

爲了使用performSegueWithIdentifier你必須建立一個segue。進入界面構建器,在用戶點擊單元格時,在表格單元格和要顯示的新視圖控制器之間拖動一個segue。選擇segue,進入身份選項卡,並用「detailViewSegue」等文本填充標識符選項卡。進入主視圖控制器並執行performSegueWithIdentifier(「detailViewSegue」,發件人:無)。然後執行prepareForSegue方法。你可以在這裏閱讀更多:http://stackoverflow.com/questions/24040692/prepare-for-segue-in-swift。你將通過這種方式來區分。 –

0

NavigationController設爲初始視圖。

  • 選擇secondViewController視圖
  • 轉到編輯器 - >嵌入在 - >導航控制器。
  • 設置identifierNavigationController使用identity inspector裏面storyboard見下圖。

enter image description here

0

1.You要嵌入您的ViewController包含在故事板你的tableview此過程

轉到故事板 - >點擊你的控制器 - >編輯器(在頂部菜單) - >嵌入 - >導航控制器

  • 寫下這個代碼把你的控制器

    let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("contact") as! contactsandlists 
    self.navigationController.pushViewController(secondViewController, animated: true) 
    
  • 相關問題