简体   繁体   English

添加“完成”按钮以关闭以编程方式创建的UIViewController

[英]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然后将rightBarButtonItemUIBarButtonItem(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. 当您将mywebViewControllermywebViewController导航控制器时,请使导航栏可见。

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.

相关问题 通过完成按钮以编程方式关闭ModalView - Dismiss ModalView Programmatically by done button 使用navbar以编程方式创建UIViewController以及完成按钮和ModalView Presentation的UITextView - Create UIViewController programmatically with navbar along with done button and UITextView for ModalView Presentation 如何添加UIViewController作为在以编程方式创建的UIView中创建的UIButton操作的目标? - How to add UIViewController as target of UIButton action created in programmatically created UIView? 将按钮添加到以编程方式创建的UIView - Add Button to UIView Created Programmatically 向以编程方式创建的按钮添加约束 - add constraint to programmatically created button 以编程方式在UIViewController中添加UINavigationController - Programmatically add UINavigationController in UIViewController 从其UIViewController中的按钮关闭UIPopoverController - Dismiss UIPopoverController from a button within its UIViewController 如何在以编程方式创建的按钮上添加微调指示器 - How to add Spinner Indicator on Programmatically created Button MPMovieController不会在“完成”按钮上关闭 - MPMovieController won't dismiss on Done button 点击完成按钮时关闭UIPickerView - Dismiss UIPickerView when tapping done button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM