-1

Is there a way to check the current UIViewController from a custom subclassed UIView?

Example

class x: UIView {
    let VC = self.superview!
    switch VC {
    case is oneVC:
        print("one")
    case is anotherVC:
        print("another")
    default:
        fatalError("We only have two VC's")
    }
}

Currently XCode is saying.

'Cast from UIView to unrelated type oneVC (anotherVC) always fails'

There is two warnings.

Cody Weaver
  • 4,756
  • 11
  • 33
  • 51
  • 2
    A view controller can't be any view's superview. Only a view can be the superview of another view. – rmaddy Oct 08 '15 at 16:44
  • 3
    BTW - it's a really bad design if the view needs to know what view controller it is in. – rmaddy Oct 08 '15 at 16:44
  • 1
    @rmaddy I need to know for a segue between view controllers. So if its from one do the correct animation for one to another. But, if its from two then I want to do the correct animation for two to another. I am doing this by calling one.segueAnimation() or two.segueAnimation() instead of the print statements that are currently there. – Cody Weaver Oct 08 '15 at 16:58
  • 1
    Don't do segues from a view. Do them in the view controller. The view should post an event saying that "hey someone, this even happened. Do what you need to do". Then each view controller responds to the event as needed and does whatever segue is appropriate. That is proper design. – rmaddy Oct 08 '15 at 17:00
  • Possible duplicate of [Get to UIViewController from UIView?](http://stackoverflow.com/questions/1340434/get-to-uiviewcontroller-from-uiview) – Ronald Martin Oct 08 '15 at 17:33

2 Answers2

0

As rmaddy says, only a UIView can be superview of a view- not a UIViewController. A view controller has a view, but it itself is not a UIView. This is why the compiler tells you that they are unrelated types.

You're on the right track as far as checking types goes. In Swift, you've already found that dynamic type checking is done using the is keyword. However, since you also want to use your subclass's implementation of segueAnimation(), the preferred method would be to use the as keyword for type casting.

    if let VC = VC as? oneVC {
        print("one")
        VC.segueAnimation() // Use oneVC's segueAnimation method
    } else if let VC = VC as? anotherVC { 
        print("another")
        VC.segueAnimation() // Use anotherVC's segueAnimation method
    } else {
        fatalError("We only have two VC's")
    }

Of course, you should still reconsider where you are putting this check. Any segue preparation and execution should be done by a view controller, not the view itself.

Ronald Martin
  • 4,278
  • 3
  • 27
  • 32
0

This should work;

if let VC:UIViewController = self.nextResponder() as? UIViewController {
    switch VC {
    case is oneVC:
        print("one")
    break

    case is anotherVC:
        print("another")
    break

    default:
        fatalError("We only have two VC's")
    }
}
Shoaib
  • 2,286
  • 1
  • 19
  • 27