将数据从TableView传递到WebView [英] Passing data from a tableview to webview

查看:113
本文介绍了将数据从TableView传递到WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是swift3的新手.因此,如果您认为这是一个简单的问题,请原谅我.在搜索Internet或出现堆栈溢出情况时并不清楚我的情况,所以我问这个问题.

I am new of the swift3. So, please forgive me if you think it is easy question. Do not have a clear idea when searching internet or stack overflow with my situation, so I ask this question.

目标:将数据从表格视图传递到网络视图

Goal: Passing data from a tableview to webview

示例:表中的数据1,2,3 ...,按1,然后跳到值为1的webview

Example: data 1,2,3... in the table, press 1, then jump into webview with value 1

信息:

  • 在main.storyboard中,如下所示:
  • 使用viewview的视图控制器的oneViewController类

  • In main.storyboard, looks like:
  • class oneViewController for a view controller with tableview

具有webview的视图控制器的twoViewController类

class twoViewController for a view controller with webview

在oneViewController中,一切设置良好,并在以下位置选择行:

In oneViewController, things are well set and select row at:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //Array[indexPath.row]   //This value expected to pass
    //some code to execute passing data...
    //Also, jump into another view controller with webview
}  

在twoViewController中,一切都准备就绪:

In twoViewController, everything got ready:

 //let passData = some code to get the data....
 let URL = NSURL(string: "https://www.example.com?data=\(passData)")

 webView.loadRequest(NSURLRequest(url: URL! as URL) as URLRequest)

最后,PHP可以获取值

Finally, PHP can get the value

echo $_GET["data"];

问题:

  1. 如何在main.storyblard中设置tableview和webview之间的关系?

将视图控制器连接到其他视图控制器吗?还是将Table view Cell连接到View Controller?或其他的东西.....

Connect view controller to anther view controller? Or connect Table view Cell to view controller? Or something else.....

  1. 如何在类oneViewController和twoViewController中实现传递数据?

推荐答案

请按照以下步骤操作:-

Follow the below steps:-

步骤1:以下代码将启动并传递TwoViewController中的数据.请注意,在Main.Storyboard-> TwoViewContoller中,将标识符指定为<​​c0>

Step 1: Below code will launch and pass the data in your TwoViewController. Please note that in your Main.Storyboard->TwoViewContoller, give the identifier as TwoViewController

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let twoVC:TwoViewController = storyBoard.instantiateViewController(withIdentifier: "TwoViewController") as! TwoViewController
    twoVC.passData = Array[indexPath.row]
    self.present(twoVC, animated: true, completion: nil)
}  

第2步:在您的TwoViewController类中,如下定义一个变量

Step 2: In your TwoViewController class define a variable as below

var passData: String = ""

这篇关于将数据从TableView传递到WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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