TableView上でセルを選択して、それぞれのセルに割り当てられたwebサイトに遷移するシステムを作っています。
画面遷移は出来るのですが、遷移後のWebViewにwebサイトが表示されません。コードを調べても解決策が分からないので教えていただきたいです。
該当のソースコード
ListViewController
1import UIKit 2 3class ListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 @IBOutlet weak var tableView: UITableView! 6 var labonames = [ 7 (name:"江木 啓訓 研究室", url:"http://egi.inf.uec.ac.jp/"), 8 (name:"大河原 一憲 研究室", url:"http://www.uec.ac.jp/research/information/opal-ring/0006120.html"), 9 (name:"大須賀 昭彦・清 雄一 研究室", url:"http://www.ohsuga.is.uec.ac.jp/"), 10 (name:"小泉 直也 研究室", url:"https://sites.google.com/site/koizuminaoya/") 11 ] 12// tableviewの処理 13 func numberOfSections(in tableView: UITableView) -> Int { 14 return 1 15 } 16 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 17 return labonames.count 18 } 19 // cellを生成して返却する 20 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 21 let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "labocell", for: indexPath) 22 let labodata = labonames[(indexPath).row] 23 cell.textLabel?.text = labodata.name 24 return cell 25 } 26 override func prepare(for segue : UIStoryboardSegue, sender: Any?){ 27 if segue.identifier == "Segue" { 28 if let indexPath = self.tableView.indexPathForSelectedRow{ 29 let labodata = labonames[(indexPath).row] 30 let controller = segue.destination as! SecondViewController 31 controller.data = labodata 32 } 33 } 34 } 35 override func viewDidLoad() { 36 super.viewDidLoad() 37 tableView.delegate = self 38 tableView.dataSource = self 39 } 40}
遷移後のViewController
SecondViewController
1import UIKit 2import WebKit 3class SecondViewController: UIViewController{ 4 @IBOutlet weak var webView: WKWebView! 5 var data:(name:String, url:String)! 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 if let myurl = URL(string: self.data.url){ 10 let urlReq = URLRequest(url: myurl) 11 webView.load(urlReq) 12 } 13 } 14 15}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/05 13:51
2019/01/05 22:52