Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UIViewController of a UIView's superview in iOS?

Tags:

I have a UIViewController in which I have a UITextView added from interface builder. Now I want to push a view when I click on hyperlink or phone number. I am able to detect that which url is clicked using a method I found in stackoverflow. Here is the method

@interface UITextView (Override) @end  @class WebView, WebFrame; @protocol WebPolicyDecisionListener;  @implementation UITextView (Override)  - (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id < WebPolicyDecisionListener >)listener {     NSLog(@"request: %@", request); } @end 

Now I want to get the viewController of the textview's superview so that I can push another viewController when I click on URL/Phone Number.

like image 334
Rahul Vyas Avatar asked Feb 22 '10 08:02

Rahul Vyas


People also ask

What is UIViewController in iOS?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

How do I get root view controller?

The root view controller is simply the view controller that sits at the bottom of the navigation stack. You can access the navigation controller's array of view controllers through its viewControllers property. To access the root view controller, we ask for the first item of the array of view controllers.


1 Answers

You can't access it directly, but you can find the next view controller (if any) by traversing the responder chain.

This is how the Three20 framework does it:

- (UIViewController*)viewController {     for (UIView* next = [self superview]; next; next = next.superview)     {         UIResponder* nextResponder = [next nextResponder];          if ([nextResponder isKindOfClass:[UIViewController class]])         {             return (UIViewController*)nextResponder;         }     }      return nil; } 
like image 86
Felixyz Avatar answered Sep 21 '22 18:09

Felixyz