Swift4.0で、tableViewを使ったアプリを作っています。
「WKViewで開く」「SFSafariViewControllerで開く」という2つのセルがあり、押すと画面遷移してそれぞれWKViewとSFSafariViewControllerでウェブページ開くような挙動を想定しています。
以下のようなコードで一応ウェブページが開くことは開くのですが、
その際「tableViewでセルを選択→画面遷移→さらにもう一度画面遷移してウェブページが開く」という動きになってしまいます。
画面遷移は一度だけになるようにしたいのですが、原因がわかりません。
WKView、SFSafariViewControllerの両方のセルで同じ症状が起こっているので、tableViewの時点で何か間違っているところがありそうだと思っています。
どこかおかしい箇所はあるでしょうか?
tableViewを制御するコードは以下です。
Swift
1import UIKit 2 3class WebListTableViewController: UITableViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 } 8 9 override func didReceiveMemoryWarning() { 10 super.didReceiveMemoryWarning() 11 // Dispose of any resources that can be recreated. 12 } 13 14 // MARK: - Table view data source 15 16 let webList = [ 17 (name: "WKWebView", url:"https://www.apple.com"), 18 (name: "SFSafariViewController", url:"https://www.apple.com") 19 ] 20 21 override func numberOfSections(in tableView: UITableView) -> Int { 22 // #warning Incomplete implementation, return the number of sections 23 return 1 24 } 25 26 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 27 // #warning Incomplete implementation, return the number of rows 28 return webList.count 29 } 30 31 32 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 33 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 34 let webData = webList[(indexPath as NSIndexPath).row] 35 cell.textLabel?.text = webData.name 36 cell.detailTextLabel?.text = webData.url 37 38 return cell 39 } 40 41 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 42 if indexPath.row == 0 { 43 performSegue(withIdentifier: "showWKWebView", sender: indexPath.row) 44 } 45 else if indexPath.row == 1 { 46 performSegue(withIdentifier: "showSFSafariViewController", sender: indexPath.row) 47 } 48 } 49 50 // MARK: - Navigation 51 52 // In a storyboard-based application, you will often want to do a little preparation before navigation 53 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 54 if segue.identifier == "showWKWebView" { 55 if let indexPath = self.tableView.indexPathForSelectedRow { 56 let webData = webList[(indexPath as NSIndexPath).row] 57 (segue.destination as! ViewController).data = webData 58 } 59 } 60 else if segue.identifier == "showSFSafariViewController" { 61 if let indexPath = self.tableView.indexPathForSelectedRow { 62 let webData = webList[(indexPath as NSIndexPath).row] 63 (segue.destination as! SFViewController).data = webData 64 } 65 } 66 } 67}
「WKViewを開く」のセルを押した後の画面は、以下のコードで定義しています。
Swift
1import UIKit 2import WebKit 3 4class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate { 5 6 @IBOutlet weak var webView: WKWebView! 7 8 var data:(name:String, url:String)? 9 10 override func loadView() { 11 super.loadView() 12 let webConfiguration = WKWebViewConfiguration() 13 webView = WKWebView(frame: .zero, configuration: webConfiguration) 14 webView.uiDelegate = self 15 webView.navigationDelegate = self 16 self.view = webView 17 } 18 19 override func viewDidLoad() { 20 super.viewDidLoad() 21 22 if let webData = data { 23 if let url = URL(string:webData.url) { 24 let urlReq = URLRequest(url:url) 25 webView.load(urlReq) 26 } 27 } 28 } 29 30 override func didReceiveMemoryWarning() { 31 super.didReceiveMemoryWarning() 32 // Dispose of any resources that can be recreated. 33 } 34}
あなたの回答
tips
プレビュー