9

I've done this to get the view:

[self.superview viewWithTag:10]

But how can I get that view's viewcontroller. Just like you can get the the viewcontroller's view I want to go the other way so I can send a message (call a method) to that viewcontroller. eg:

[[self.superview viewWithTag:10].viewController doSomething];

(obviously that not actual code but I want something like that)

Beraliv
  • 489
  • 7
  • 18
Jonathan.
  • 53,471
  • 47
  • 180
  • 281

2 Answers2

22

You can use the -nextResponder method to do it:

[(YourUIViewController *)[[self.superview viewWithTag:10] nextResponder] doSomething];

According to http://developer.apple.com/library/ios/documentation/uikit/reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/nextResponder , "UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn’t)"

user102008
  • 29,685
  • 10
  • 80
  • 102
12

you can use

[yourUIView.window.rootViewController doSomething]
itecheck
  • 129
  • 1
  • 2
  • some further details answers on that ... http://stackoverflow.com/a/2596519/294884 may help someone – Fattie Apr 14 '14 at 14:12