いつもお世話になっております。
UITabBarControllerのtabBarItemのタップを検知して、
タップしたらUIWebViewのloadRequestメソッドを呼び出したいと思っています。
UIWebViewはViewController.swiftで定義しています。
UITabBarControllerのtabBarItemのタップ検知は、
MyTabBarController.swift内のtapHandler関数でおこなっています。
tapHandler関数内で、ViewControllerクラスのwebViewを引っ張ってきて、
loadRequestを記載し、シミュレーターで実行すると、
何も動きはなく、コンソールにload failed (error code: -999 [1:89])
というエラーが表示されます。
別クラスで定義されているインスタンスの使い方を間違っているのか、
webViewの使い方を間違っているのか、調べてみたのですが実現方法がわかりませんでした。
ご教示頂けますと幸いです。
不足情報等、ございましたらご指摘ください。
// ViewController.swift import UIKit class ViewController: UIViewController { public let webView = UIWebView() override func viewDidLoad() { super.viewDidLoad() webviewDisplay() } func webviewDisplay() { webView.frame = self.view.bounds webView.scalesPageToFit = true self.view.addSubview(webView) let url:URL = URL(string: "https://www.google.co.jp/")! let request: URLRequest = URLRequest(url: url) webView.loadRequest(request) } }
// MyTabBarController.swift import UIKit import RAMAnimatedTabBarController class MyTabBarController: RAMAnimatedTabBarController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func tapHandler(_ gesture: UIGestureRecognizer) { super.tapHandler(gesture) print(selectedIndex) //print 0 if you tap tabBarItem of FirstViewController switch selectedIndex { case 0: print("0です") let url = URL(string: "https://apple.com") let request = URLRequest(url: url) ViewController().webView.loadRequest(request) case 1: print("1です") case 2: print("2です") case 3: print("3です") default: break } } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/21 15:49
2017/10/21 21:14