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
Example: data 1,2,3... in the table, press 1, then jump into webview with value 1
Information:
- In main.storyboard, looks like:
class oneViewController for a view controller with tableview
class twoViewController for a view controller with webview
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
}
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)
Finally, PHP can get the value
echo $_GET["data"];
Questions:
- How to set the relationship between tableview and webview in main.storyblard?
Connect view controller to anther view controller? Or connect Table view Cell to view controller? Or something else.....
- How to implement passing data in class oneViewController and twoViewController?
you can do three solutions :
1- send data in segue
2- forget the segue relation and call the new controller in didSelectRowAt, and set data like this :
3- use struct like this and you be able to use your data from any place in your project :
for exemple :
Variables.passData=@"new value"
Follow the below steps:-
Step 1: Below code will launch and pass the data in your
TwoViewController
. Please note that in your Main.Storyboard->TwoViewContoller, give the identifier asTwoViewController
Step 2: In your
TwoViewController
class define a variable as below