[英]In swift iOS 8 when a cell is clicked, how do I get it to open another view controller?
I'm fairly new to swift programming, and been trying for hours . 我刚开始进行快速编程,已经尝试了几个小时。 Any help is well appreciated.
任何帮助都将不胜感激。
Just override: didSelectRowAtIndexPath
只是覆盖:
didSelectRowAtIndexPath
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath path: NSIndexPath!) {
let secondViewController =
self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController")
as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
}
To expand on @Epic Defeater's comment, if your table view controller and the destination view controller is in the same storyboard, control-drag from the table cell to the destination view controller. 若要扩展@Epic Defeater的注释,如果您的表视图控制器和目标视图控制器在同一情节提要中,请从表单元格将控件拖动到目标视图控制器。 This is a segue, and it does what @Klevison Matias's code does, but with less involvement from you.
这是一个问题,它可以执行@Klevison Matias的代码,但是您的参与较少。
The segue needs a unique identifier, and in your table view controller, you must implement prepareForSegue:sender:
. segue需要一个唯一的标识符,并且在表视图控制器中,您必须实现
prepareForSegue:sender:
You need to create an instance of the View Controller that you want to open and then transition to that view controller - presumably, by 'pushing' that View Controller onto the navigation stack (navigation controller). 您需要创建要打开的View Controller的实例,然后过渡到该View Controller-大概是通过将View Controller“推”到导航堆栈(导航控制器)上。
For example... 例如...
let nextViewController: MyViewController = MyViewController()
self.navigationController.pushViewController(nextViewController, animated:true)
只需将单元格中的segue添加到要与其连接的视图控制器即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.