简体   繁体   English

在快速的iOS 8中,当单击一个单元格时,如何获取它以打开另一个视图控制器?

[英]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.

相关问题 在Swift中单击Table View的单元格时如何调用(显示)另一个View Controller - How to call (show) another View Controller when clicked on the cell of Table View in Swift 如何在Swift iOS开发中从一个视图控制器到另一个容器视图控制器获取分段控制索引? - How do I get the Segmented Control index from one view controller to another containerview controller in Swift iOS development? 通过单击表视图中的单元格 - Swift iOS打开新的视图控制器 - Open new View Controller by clicking Cell in Table View - Swift iOS iOS-如何在我的应用启动时将另一个视图控制器设置为我的初始视图控制器? - iOS - How do I set another view controller to be my initial view controller when my app starts? 在 Swift 中单击表视图单元格时如何显示新的视图控制器 - How to show new View Controller when a Table View Cell is clicked in Swift 如何判断从另一个View Controller单击哪个表格单元格? IOS 7 - How to tell which table cell was clicked from another View Controller? iOS 7 Swift iOS - 当作为子视图控制器添加到另一个视图控制器时,是否应该在子视图控制器中调用 Deinit? - Swift iOS -Should Deinit get called inside child View Controller when added as a child to another View Controller? 如何在Swift中向另一个内部呈现视图控制器? - How do I present a view controller inside another in Swift? 如何将信息从一个视图控制器中的表视图单元传递到另一视图控制器? - How do I pass information from a table view cell in one view controller to another view controller? 当我轻按任何集合视图单元格时(不点击最后一个单元格),如何移动另一个视图控制器? - How to move another view controller when i tapped any collection view cell (without taping last cell)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM