Reputation: 289
I have a tableView which launches a URL after the cell was pressed. I am creating webView for it:
var webView = UIWebView(frame: self.view.bounds)
webView.scalesPageToFit = true
view.addSubview(webView)
let request = URLRequest(url: catPictureURL!)
webView.loadRequest(request)
webViewDidStartLoad(webView)
And I have a function to create a close button in it:
func webViewDidStartLoad(_ webView: UIWebView){
print("JUHUUU")
var subView = UIView()
subView.frame.size.height = 10
subView.frame.size.width = 10
subView.frame = CGRect(x: 0, y: 50, width: 100 , height: 100)
let button = UIButton(type: .system)
button.frame = CGRect(x: 0, y: 50, width: 100, height: 100)
button.backgroundColor = UIColor.red
button.setTitle("CANCEL", for: [])
button.addTarget(self, action: #selector(webView.removeFromSuperview), for: UIControlEvents.touchUpInside)
self.view.addSubview(button)
webView.addSubview(subView)
//subView.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "dismiss")
}
I have tried in button.addTarget to close the webView and show previous screen, but the
action: #selector(webView.removeFromSuperview)
is removing rootView
- not the webView
(which is causing crashes). I don't really know how to get webView
closed.
PS. I have also tried doing #selector("dismiss")
and preparing a function with self.dismiss
but the result is the same.
Upvotes: 2
Views: 3248
Reputation: 289
Self should be changed to webView in order to close it.
button.addTarget(webView, action: #selector(webView.removeFromSuperview), for: UIControlEvents.touchUpInside)
Upvotes: 1
Reputation: 521
Your actions target is self( which is your vc) . Set target to webView
Upvotes: 1