我是 swift3 的新手。所以,如果你认为这是一个简单的问题,请原谅我。以我的情况在网上搜索或者stack overflow的时候不是很清楚,所以才问这个问题。
目标:将数据从 tableview 传递到 webview
例子:表格中的数据1,2,3...,按1,然后跳转到值为1的webview
信息:
- 在 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"];
问题:
- 如何在main.storyblard中设置tableview和webview的关系?
将 View Controller 连接到另一个 View Controller ?或者将 TableView 单元格连接到 View Controller ?或者别的……
- 如何在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/