![](/img/trans.png)
[英]Dismiss Current View controller and open new view controller - ios Swift
[英]Open new View Controller by clicking Cell in Table View - Swift iOS
我有以下onClick函数
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
println("Row: \(row)")
println(meetingArray[row] as! String)
}
打印出单击的单元格上的文本。 它工作正常。
我只是想知道如何设置一个函数来引导你到新的视图控制器
编程方式:
let destination = UIViewController() // Your destination
navigationController?.pushViewController(destination, animated: true)
故事板:
首先,您必须为视图设置标识符。 在下面的屏幕截图中,您可以看到输入标识符的位置。
之后,您可以使用以下代码创建“目标”并将其推送到导航控制器:
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let destination = storyboard.instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
navigationController?.pushViewController(destination, animated: true)
Segue公司:
首先,您必须为segue设置标识符,如下所示:
performSegue(withIdentifier: "segue", sender: self)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
// Setup new view controller
}
}
编辑:更新为Swift 3.x
在storyboard中,在Identity Inspector中设置viewController的storyboardId。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
println("Row: \(row)")
println(meetingArray[row] as! String)
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("storyBoardIdFor your new ViewController") as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
}
对于遇到此问题的其他用户,如果UITableViewController
有静态单元格,则只需控制从单元格拖动到新的View Controller。
如果您不需要将任何数据传递到下一个View Controller,也可以在动态单元格上执行此操作。 但是,如果您确实需要传递数据,那么您应该从Table View的视图控制器本身而不是单元格中创建segue 。 然后以编程方式调用segue。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "mySegueIdentifier", sender: self)
}
试试这个对我有用,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number \(indexPath.section).")
print("Cell cliked value is \(indexPath.row)")
if(indexPath.row == 0)
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(controller, animated: true)
}
}
希望这对某些人有所帮助。
如果使用,
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
所选行的值太晚传递给另一个视图控制器。 您最好在func prepareForSegue中传递所选行,如下所示,首先声明一个全局变量以将值传递给其他viewController
var globalVar:Int
在基于故事板的应用程序中,您通常需要在导航之前做一些准备工作
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
let selectedIndex = self.tableView.indexPathForCell(sender as! UITableViewCell)
globalVar = selectedIndex!.row
print("tableView prepareForSegue: " + String(globalVar))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.