行いたいこと
下記のTableView第1画面よりセルをタップして第2画面のSafari画面に遷移すること目論んでいます。
TableView第1画面
第2画面のSafari画面
注:この画像はあとからSafariを表示してスクリーンショットを撮ったものです。コードで表示された画面には左上に完了ボタンが表示されましたが、ボタンを押してもSafariから抜けることはできませんでした。
行ったこと
1個のURLを DetailTomzyViewController.swift上にコーディングして、第1画面のセルをタップしてSafari画面に遷移し、ひとつのURLを表示させることは成功しました。
しかし、 ViewTomzyControllerJp.swift上に下記のコードのごとく6個のURLをアレイで記載して第1画面のセルをタップしてSafari画面に遷移し、6個のURLを表示させることができていません。
それぞれのファイル上のコードは下記のとおりです。エラー表示も下記に記載します。
ViewTomzyControllerJp.swiftのコード
// VoiceAsisst01Free // // Created by Tomiji Suzuki on H30/04/03. // Copyright © 平成30年 Tomiji Suzuki. All rights reserved. // import Foundation import UIKit import WebKit class ViewTomzyControllerJp: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet var table:UITableView! // section毎の画像配列 let imgArray: NSArray = ["Video_r.png","Video_r.png","Book_g.png","Book_g.png","Book_g.png","Book_g.png"] let label1Array: NSArray = ["操作方法(動画)","ゲームの遊び方(動画)","このアプリについて","Q & A","トミ爺アプリについて","開発者について"] let textsURL = ["http://tomzyapp.com/voiceinput02/01.html", "http://tomzyapp.com/voiceinput02/02.html", "http://tomzyapp.com/voiceinput02/03.html", "http://tomzyapp.com/voiceinput02/04.html", "http://tomzyapp.com/voiceinput02/05.html", "http://tomzyapp.com/voiceinput02/06.html"] var selectedIndexPath: IndexPath! override func viewDidLoad() { super.viewDidLoad() } //Table Viewのセルの数を指定 func tableView(_ table: UITableView, numberOfRowsInSection section: Int) -> Int { print ("Tomzy01セルの数を指定") return imgArray.count } //各セルの要素を設定する func tableView(_ table: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // tableCell の ID で UITableViewCell のインスタンスを生成 let cell = table.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) let img = UIImage(named: imgArray[indexPath.row] as! String) // Tag番号 1 で UIImageView インスタンスの生成 let imageView = cell.viewWithTag(1) as! UIImageView imageView.image = img // Tag番号 2 で UILabel インスタンスの生成 let label1 = cell.viewWithTag(2) as! UILabel label1.text = String(describing: label1Array[indexPath.row]) print ("Tomzy02セルの要素を設定") return cell } // Cell の高さを120にする func tableView(_ table: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat { print ("Tomzy03セルの高さを指定") return 70.0 } //////上記のTableViewの表示は成功している/////// //ここから後は第2画面に遷移してウェブサイトSafariを表示する //テーブルビューのセルがタップされた処理 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print ("Tomzy04タップセルが選択された") selectedIndexPath = indexPath //cellが選択された場合 //toDetailViewへ遷移するためにsegueを呼び出す performSegue(withIdentifier: "toDetailView", sender: self) print ("Tomzy05セルがタップされた") } override func prepare(for segue: UIStoryboardSegue, sender: Any!) { if (segue.identifier == "toDetailView") { let subVC = (segue.destination as? DetailTomzyViewController)! // print ("Tomzy06画面が遷移した") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
DetailTomzyViewController.swift上のコード
// VoiceAsisst01Free // // Created by Tomiji Suzuki on H30/03/21. // Copyright © 平成30年 Tomiji Suzuki. All rights reserved. // import Foundation import UIKit import SafariServices class DetailTomzyViewController: UIViewController { var urlString : String? override func viewDidLoad() { super.viewDidLoad() } override func viewDidAppear(_ animated: Bool) { //渡されたURLをWebViewにリクエストする let url = URL(string: urlString!) let request = URLRequest(url: url!) let safariVC = SFSafariViewController(url: NSURL(string: request)! as URL) present(safariVC, animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func safariViewControllerDidFinish(controller: SFSafariViewController) { print("close") } }
エラー表示
Cannot convert value of type 'URLRequest' to expected argument type 'String'
エラーの位置
教えて頂きたいとこ(2件)
1.上記エラーを解消してSafari画面に遷移し、6個のURLを表示させるコードを教えてください。
2.第2画面のSafariが表示されたあと、完了ボタンを押したとき、元のアプリに戻るコードは
func safariViewControllerDidFinish(controller: SFSafariViewController) {
であると見つけたのですが、第1画面に戻るコードはどのように書いたらよいでしょうか。
ご参考事項:
環境は下記のとおりです。
MacBook Pro (15-inch, 2016)
High Sierra OS10.13.2
Version 9.2 (9C40b)Swift4にバージョンアップしました。
「音声入力アシスト」は既に2017.12にリリースできましたが、現在機能を充実し、更に英語を標準として日本語をローカリゼーションしている最中です。よろしくお願いします。
なお、SFSafariViewControllerに関連して
リンク内容[https://i-app-tec.com/ios/web-browser.html#2]
を参照しました。
回答1件
あなたの回答
tips
プレビュー