[英]Add a “Done” button to dismiss a programmatically created UIViewController
I'm a swift newbie and made a demo app which has a UIViewController and UITableViewController in the Storyboard. 我是一个快速的新手,并制作了一个演示应用程序,在故事板中有一个UIViewController和UITableViewController。 The UIViewController has a Collection View and an embedded navigation controller. UIViewController有一个Collection View和一个嵌入式导航控制器。 On tapping a collection view cell, the app segues to the UITableViewController. 在点击集合视图单元格时,应用程序将转到UITableViewController。 I can come back to my UIViewController from the UITableViewController as well. 我也可以从UITableViewController回到我的UIViewController。 All this works well. 这一切都运作良好。 Now, some of the rows in my UITableViewController have URLs. 现在,我的UITableViewController中的一些行有URL。 On tapping a URL, I programmatically create a webview inside a UIviewcontroller and get the URL to load in it. 在点击URL时,我以编程方式在UIviewcontroller中创建webview并获取要加载的URL。 The issue is that I can't dismiss the webview and get back to my UITableViewController after this. 问题是我无法关闭webview并在此之后返回到我的UITableViewController。 This is the code I have in my UITableViewController class : 这是我在UITableViewController类中的代码:
// When user taps a row, get the URL and load it in a webview
let mywebViewController = UIViewController()
let webView = UIWebView(frame: mywebViewController.view.bounds)
webView.loadRequest(NSURLRequest(URL: url)) // url from the row a user taps
mywebViewController.view = webView
self.navigationController!.presentViewController(mywebViewController, animated: true, completion: nil)
How can I add a Done button to this programmatically created UIviewcontroller to dismiss the webview and get back to my UITableViewController 如何向这个以编程方式创建的UIviewcontroller添加“完成”按钮以关闭webview并返回到我的UITableViewController
You can wrap the mywebViewController
in a UINavigationController
then set the rightBarButtonItem
to UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "dismiss")
like this: 您可以将mywebViewController
包装在UINavigationController
然后将rightBarButtonItem
为UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "dismiss")
如下所示:
let navController = UINavigationController(rootViewController: mywebViewController)
mywebViewController.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "dismiss")
self.navigationController!.presentViewController(navController, animated: true, completion: nil)
and add a dismiss
function in your UITableViewController
: 并在UITableViewController
添加一个dismiss
函数:
func dismiss() {
self.dismissViewControllerAnimated(true, completion: nil)
}
Add done button to your presented ViewController and make an action for it. 将完成按钮添加到您呈现的ViewController并为其执行操作。 To dismiss ViewController try this : 要解雇ViewController,请尝试以下方法:
self.dismissViewControllerAnimated(true, completion: nil)
在mywebViewController
和@IBAction
中调用添加按钮
self.dismissViewControllerAnimated(true, completion: nil)
You can do one thing. 你可以做一件事。
As you are pushing your mywebViewController
into the navigation controller, make the navigation bar visible. 当您将mywebViewController
推mywebViewController
导航控制器时,请使导航栏可见。
By simply tapping on Back button in the navigation bar you can easily dismiss your mywebViewController
. 只需点击导航栏中的“返回”按钮,即可轻松关闭mywebViewController
。
Hope this helps you. 希望这对你有所帮助。 :) :)
Update for Swift 4 & 5 Swift 4和5的更新
In the VC you're navigating from 在你导航的VC中
navController.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.dismiss))
Have to declare your selector as an @objc func 必须将您的选择器声明为@objc函数
@objc func dismiss(){
self.dismiss(animated: true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.