Saturday, July 24, 2010

Getting UIViewController from UIView

Some time we need reference of view controller from view. While searching for solution I found that view's nextResponder method returns view controller.

In my application I required superview's view controller, Following is code for achieving same.

id superView = [self superview];
id viewController = [superView nextResponder];
[viewController doSomeStuff];

And here is what UIResponder documentation says about nextResponder

UIView implements this method by returning theUIViewController object that manages it (if it has one) or its superview (if it doesn’t); UIViewController implements the method by returning its view’s superview; UIWindow returns the application object, and UIApplication returns nil.

No comments:

Post a Comment