找到你要的答案

Q:Back to previous ViewController from class func

Q:从类功能回到以前的ViewController

I try to close and go back to previous viewController using:

class func closeViewController()
{
    presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}

But this gives me a compilation error.

if i remove the "class" identifyer it works, but i need this function to be class function

我试着靠近,回不到以前的视图的使用:

class func closeViewController()
{
    presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}

但这给了我一个编译错误。

如果我去掉“类”identifyer它工作,但我需要这个函数是类函数

answer1: 回答1:

Just because you receive a callback from another class doesn't mean that you need your closeViewController method to be a class method. It's likely that it should not be a class method.

presentingViewController is an instance method of UIViewController. There is an implied "self" at the beginning:

self.presentingViewController?.dismissViewControllerAnimated(
  true, completion: nil)

However, a class method is performed on the class, not an instance, so self is the class, not an instance.

If you really do need it to be a class method for some reason, you will need to pass in either the current view controller or the presenting view controller as a parameter to the method.

class func closeViewController(theModal: UIViewController)
{
  theModal.dismissViewControllerAnimated(
    true, completion: nil)
}

只是因为你收到回调从另一类并不意味着你需要你的closeviewcontroller方法是一个类的方法。它可能不应该是一个类的方法。

presentingviewcontroller是处理一个实例方法。一开始就有一个隐含的“自我”:

self.presentingViewController?.dismissViewControllerAnimated(
  true, completion: nil)

但是,类方法在类上执行,而不是实例,所以自是类,而不是实例。

如果您确实需要它作为某种原因的类方法,则需要将当前视图控制器或呈现视图控制器传递给该方法的参数。

class func closeViewController(theModal: UIViewController)
{
  theModal.dismissViewControllerAnimated(
    true, completion: nil)
}
answer2: 回答2:

try this code :

self.dismissViewControllerAnimated(true, completion: nil)

尝试此代码:

self.dismissViewControllerAnimated(true, completion: nil)
ios  swift  uiviewcontroller