简体   繁体   English

如何使用Swift关闭ios中的viewController

[英]How to dismiss viewController in ios using Swift

I have a viewController which contains only an imageview. 我有一个viewController,它只包含一个imageview。 I want to present it whenever there is a loading in the application like fetching data from a webservice. 我想在应用程序中加载时提供它,例如从Web服务获取数据。 So I have created a function in my loaderViewController as 所以我在我的loaderViewController中创建了一个函数

func showLoading(viewController:UIViewController) {
    viewController.presentViewController(LoadingViewController(), animated: false, completion: nil)
}

This is working as expected, when I call this function when desired like below 这是按预期工作的,当我在需要时调用此函数,如下所示

var loader = LoadingViewController()
loader.showLoading(self)

It show me the viewController with image. 它向我展示了带有图像的viewController。

But Now want to dismiss this viewController when desired but I am not able to do so, This is what I have tried so far, I created another function in my LoaderViewController as 但是现在想要在需要的时候忽略这个viewController,但我无法这样做,这是我到目前为止所尝试的,我在我的LoaderViewController中创建了另一个函数as

func dismissLoader() {
    let load = LoadingViewController()
    load.dismissViewControllerAnimated(true) {
        print("Dismissing Loader view Controller")
    }
}

But its not working and the viewController is not disappering from the screen. 但它没有工作,viewController没有从屏幕上消失。

Please guide me 请指导我

You don't have to create a new instance of your loader and call dismissViewControllerAnimated(_:Bool) on it. 您不必创建加载器的新实例,并在其上调用dismissViewControllerAnimated(_:Bool)

Just call 打电话吧

self.dismissViewControllerAnimated(true)

on your viewController 在你的viewController上

So, your function will be 所以,你的功能将是

func dismissLoader() {

    dismissViewControllerAnimated(true) {
        print("Dismissing Loader view Controller")
    }
}

在Swift 3中,您可以执行以下操作。

self.dismiss(animated: true)

不要每次都let load = YASLoadingViewController() ,你创建不同的控制器做一次,然后只使用load来解雇或呈现

self.dismissViewControllerAnimated(false, completion: nil)

Your code has many flaws. 你的代码有很多缺陷。 The way you trying to achieve this is not a good practice. 你试图实现这一目标的方式并不是一个好习惯。 However ,If you want a quick fix, and just want to modify your existing method do this, 但是,如果您想快速修复,只想修改现有方法,请执行此操作,

func dismissLoader() {
    self.dismissViewControllerAnimated(true) 
    print("Dismissing Loader view Controller")
}

And when you're presenting a new LoadingViewController , keep a reference to it, so you can call above method. 当你呈现一个新的LoadingViewController ,保持对它的引用,这样你就可以调用上面的方法。

Anyways, above code should work even without you holding a reference, since iOS delegate this method back to it's parent ViewController of it's hierarchy, if there's no presented ViewController available on the called ViewController. 无论如何,上面的代码应该可以工作,即使没有你持有引用,因为iOS将这个方法委托给它的层次结构的父ViewController,如果在被调用的ViewController上没有提供的ViewController。

You have to store a link for LoadingViewController in parent view controller: 您必须在父视图控制器中存储LoadingViewController的链接:

var loader: LoadingViewController?

func showLoadingIn(viewController: UIViewController) {
   loader = LoadingViewController() // create new instance before presentation
   viewController.presentViewController(loader!, animated: false, completion: nil)
}

func dismissLoader() {
    loader?.dismissViewControllerAnimated(true) {
        print("Dismissing Loader view Controller") 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM