Michael A
Michael A

Reputation: 5850

Back to previous ViewController from class func

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

Upvotes: 0

Views: 58

Answers (2)

Duncan C
Duncan C

Reputation: 131501

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)
}

Upvotes: 4

Jay Bhalani
Jay Bhalani

Reputation: 4171

try this code :

self.dismissViewControllerAnimated(true, completion: nil)

Upvotes: 0

Related Questions