代码之家  ›  专栏  ›  技术社区  ›  Rahul Vyas

如何在iOS中获取uiview的超级视图的uiviewcontroller?

  •  24
  • Rahul Vyas  · 技术社区  · 15 年前

    我有一个uiviewController,其中有一个从接口生成器添加的uitextView。现在,当我单击超链接或电话号码时,我想推送一个视图。我可以使用在StackOverflow中找到的方法检测单击哪个URL。这是方法

    @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
    

    现在我想得到textview的超级视图的viewcontroller,这样我可以在单击url/电话号码时再推一个viewcontroller。

    3 回复  |  直到 6 年前
        1
  •  76
  •   Felixyz    13 年前

    您不能直接访问它,但是您可以通过遍历响应器链找到下一个视图控制器(如果有)。

    就是这样 the Three20 framework 是这样吗?

    - (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;
    }
    
        2
  •  1
  •   kennytm    15 年前

    请注意 -webView:decidePolicyForNavigationAction:... 是一种未记录的方法(对于iPhoneOS而言)。它被记录在MacOSX上),如果是AppStore,这个应用程序可能会被拒绝。


    视图控制器未与视图关联。仅适用于倒车。要访问视图控制器,请使其成为全局可访问的变量或属性。

    如果通常使用Interface Builder,则可以定义连接到导航视图控制器的应用程序委托的出口。然后你可以用

    MyAppDelegate* del = [UIApplication sharedApplication].delegate;
    [del.the_navigation_view_controller pushViewController:...];
    
        3
  •  -1
  •   Prabhjot Singh Gogana    11 年前

    您可以从uiview获取uiviewcontroller

    UIViewController *viewC = (UIViewController *)view->_viewDelegate;