2016-09-16 33 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) 
    
  • 相关问题