ios - 将数据从 tableview 传递到 webview

标签 ios swift3

我是 swift3 的新手。所以,如果你认为这是一个简单的问题,请原谅我。以我的情况在网上搜索或者stack overflow的时候不是很清楚,所以才问这个问题。

目标:将数据从 tableview 传递到 webview

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

信息:

  • 在 main.storyboard 中,看起来像:
    main.storyboard
  • 用 tableview 为 View Controller 类 oneViewController

  • 为带有 webview 的 View Controller 类 twoViewController

在 oneViewController 中,一切都已设置好并选择行:

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中,一切准备就绪:

 //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可以拿到值

echo $_GET["data"];

问题:

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

将 View Controller 连接到另一个 View Controller ?或者将 TableView 单元格连接到 View Controller ?或者别的……

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

最佳答案

请按照以下步骤操作:-

第 1 步:下面的代码将启动并传递 TwoViewController 中的数据。请注意,在您的 Main.Storyboard->TwoViewContoller 中,将标识符指定为 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 类中定义一个变量,如下所示

var passData: String = ""

关于ios - 将数据从 tableview 传递到 webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40288974/

相关文章:

ios - 使用 Swift 查找用户是否已显示在 TableView 中

ios - 长按并拖动时放大 UICollectionViewCell

swift - Swift 3 的二元运算符

ios - ASIHTTPREQUEST 根据请求更改 View Controller 已完成

ios - 使用无人值守的 xcodebuild 管理钥匙串(keychain)以协同设计 macOS/iOS 应用程序

iOS Swift 将 Realm 对象存储到 UserDefaults.standard

ios - swift 3 : Can not convert value of type 'int' to expected argument type 'DispatchQueue.GlobalQueuePriority'

html - Twitter 卡片/打开图表/Apple 触摸图标/每一页?

ios - 位置管理器 :didChangeAuthorization vs locationManagerDidChangeAuthorization

java - 使用 Javapns 的 Apple 推送通知服务