views:

1686

answers:

2

Hello all, 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.

+1  A: 

Please note that -webView:decidePolicyForNavigationAction:... is an undocumented method (for iPhoneOS anyway. It's documented for Mac OS X) and the app will likely be rejected if that's for AppStore.


A view controller is not associated with a view. Only reverse applies. To access a view controller, make it a globally accessible variable or property.

If interface builder is used usually one could define an outlet to the application delegate that connects to the navigation view controller. Then you can use

MyAppDelegate* del = [UIApplication sharedApplication].delegate;
[del.the_navigation_view_controller pushViewController:...];
KennyTM
+8  A: 

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;
}
Felixyz