通过单击表视图中的单元格 - Swift iOS打开新的视图控制器 [英] Open new View Controller by clicking Cell in Table View - Swift iOS

查看:111
本文介绍了通过单击表视图中的单元格 - Swift iOS打开新的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下onClick函数

I have the following onClick function

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

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    let row = indexPath.row
    println("Row: \(row)")

    println(meetingArray[row] as! String)

}

打印出单击的单元格上的文本。它运行正常。

which prints out the text on the cell which is clicked. It is working fine.

我只是想知道你将如何设置一个函数来引导你到新的视图控制器

I'm just wondering how you would set a function which would direct you to the new view controller

推荐答案

以编程方式:

let destination = UIViewController() // Your destination
navigationController?.pushViewController(destination, animated: true)

故事板:

首先,您必须为视图设置标识符。在下面的屏幕截图中,您可以看到输入标识符的位置。

Storyboard:
First you'll have to set an identifier for your view. In the screenshot below you can see where to enter the identifier.

之后,您可以使用以下代码创建目标并将其推送到导航控制器:

After that, you can create a "destination" and push it to the navigation controller by using the code below:

let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let destination = storyboard.instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
navigationController?.pushViewController(destination, animated: true)

Segue:

首先,您必须为您的segue设置一个标识符,如下所示:

Segue:
First you'll have to set an identifier for your segue as shown below:

performSegue(withIdentifier: "segue", sender: self)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segue" {
        // Setup new view controller
    }
}

编辑:更新为Swift 3.x

这篇关于通过单击表视图中的单元格 - Swift iOS打开新的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆