如何从 UIView 导航到 UIViewController

Posted

技术标签:

【中文标题】如何从 UIView 导航到 UIViewController【英文标题】:How to navigate from UIView to UIViewController 【发布时间】:2013-03-21 07:36:52 【问题描述】:

我知道 uiview 没有导航控制。因此,我无法从 uiview 推送到下一个 uiviewcontroller。

这是我的方法的代码。请帮忙。谢谢!

 - (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer 
    CGPoint pointInView = [recognizer locationInView:self];
    NSLog(@"%f , %f",pointInView.x,pointInView.y);

    if ((pointInView.x >= (231.0) && pointInView.x <=(293) && pointInView.y >= (193)  && pointInView.y <=(218)) )
    

        // how to push to next viewcontroller here?

        UIView *view = [UIGestureRecognizer view]; //what should i do for this code?


        [self viewController];
    


-(TestViewViewController*)viewController

    for (UIView *next = [self superview]; next; next = next.superview)
    
        UIResponder *nextResponder = [next nextResponder];

        if([nextResponder isKindOfClass:[TestViewViewController class]])
        

            return (TestViewViewController*)nextResponder;
        
    

    return nil;

【问题讨论】:

【参考方案1】:

如果你想让 viewcontroller 有一个视图,你可以使用nextResponder 方法

所以首先你从gestureRecognizer获取视图:

UIView *view = [gestureRecognizer view];

然后搜索包含视图的视图控制器

Look here

编辑:

 if ((pointInView.x >= (231.0) && pointInView.x <=(293) && pointInView.y >= (193)  && pointInView.y <=(218)) )
    

        // how to push to next viewcontroller here?

        UIView *view = [recognizer view]; //what should i do for this code?

       UIViewController *viewController = [self getViewControllerFromView:view];

       if(viewController) 
          // do whatever you want...
         UIViewController *newViewController = ...
         [viewController.navigationController pushViewController:newViewController animated:YES];
       

    

... 我为您的案例重写了 get ViewController 方法。从某种角度来看:

-(UIViewController*)getViewControllerFromView:(UIView*) view

    if([[view nextResponder] isKindOfClass:[UIViewController class]])
    
        return (UIViewController*)[view nextResponder];
    
    else 

        for (UIView *next = [view superview]; next; next = next.superview)
        
            UIResponder *nextResponder = [next nextResponder];

            if([nextResponder isKindOfClass:[UIViewController class]])
            
                return (UIViewController*)nextResponder;
            
        
    

    return nil;

新编辑::

你的EAGLView@property (nonatomic, retain) UIViewController *viewController;@synthesize viewController = _viewController; 添加到.m 文件中。

CoreMotionAppDelegate::

MainViewController * itemsViewController = [[MainViewController alloc]init];

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:itemsViewController];
    glView.viewController = itemsViewController;  


    [glView startAnimation];
    [[self window]setRootViewController:navController];

比你的 scrollViewDoubleTapped 方法只是

EAGLView *v = (EAGLView*)[recognizer view];  
UIViewController *viewController =  v.viewController;

【讨论】:

嗨,乔治,我按照您的建议编辑了代码,但我不确定如何将 uiview 链接到响应者。我编辑了上面的代码。请看一下。谢谢! 我看到你根本不明白你在做什么。等等我会编辑答案 嗨,乔治。我将您的代码添加到我的项目中,并在 // 随心所欲地添加了这两行代码。 TestViewViewController *newViewController = [[TestViewViewController alloc]initWithNibName:nil bundle:nil]; [viewController.navigationController pushViewController:newViewController 动画:YES];但我遇到了一些错误:'NSInvalidArgumentException',原因:'+[UIGestureRecognizer view]: unrecognized selector sent to class 0x3f342908' 请帮忙。谢谢乔治! 我的错,因为在浏览器中写了代码,替换[UIGestureRecognizer view];到[识别器视图]; 嗨乔治,我已经添加了上面的代码,但是我仍然无法推送到新的 UIViewController。 My view = EAGL View 包含一个 3D 模型,当用户触摸模型的某个部分时,它会链接到新的 UIViewController,是否可以这样做?

以上是关于如何从 UIView 导航到 UIViewController的主要内容,如果未能解决你的问题,请参考以下文章

如何从 UIView 类的 xib 文件导航到 ViewController

如何从 UIView 导航 UIViewController

导航离开时如何从一个 ViewController 上的 UIView 子视图中处理

从@IBDesignable UIView didSelectRowAt UITableView 导航到 ViewController

如何旋转两个 UIView?

如何使用 PushViewController 方法从 UIViewClass 导航到 UIViewController 类