【问题标题】:Pushing a UIViewController from a UIView从 UIView 推送 UIViewController
【发布时间】:2012-04-01 10:10:43
【问题描述】:

我需要将UIView 推送到我的UINavigation controller。我是这样做的

[self.view addSubview:showContactFlow];

在 UIView 中单击按钮时,我需要将另一个 UIViewController 推到 UIView 上。从UIView 我无法访问self.navigationcontroller 我该怎么做?

编辑:

我已将UIView 设置为我正在推进的新UIViewController 的视图,即前面提到的UIViewController。现在我想知道,如何处理其UIViewController中的UIView按钮事件,它设置在哪个视图中。

【问题讨论】:

  • 这不是正确的做法,视图和视图控制器是不同类型的对象。视图控制器确实有一种 uiviews 没有的层次结构。您实际上不能从普通视图控制器推送导航控制器(在 ios5 中您可以这样做,但在逻辑上不正确)。您需要在要显示的视图控制器内部创建一个 uinavigationcontroller,然后以模态方式显示它。如果你只是想模拟推送动画的行为,你可以简单地使用核心动画或仅使用 uiview 的动画。
  • 好的。我得到它。我只想知道,如何处理来自其父级UIViewController 的UIView's 按钮事件。
  • Xavi 你能试着更好地解释这个问题吗,因为我不明白你是如何设置应用程序的。因为对我来说 push 意味着[self.navigationController pushViewController: vc animated:YES] 的动作。没听懂,抱歉
  • 我的主要问题是我有一个子类UIView 并且我必须在我的UINavigationController 中有那个UIView。我必须从“UIView”加载另一个UIViewController。按照您的建议,我创建了一个没有 xib 的新 UIViewController,并添加了“UIView”作为新 UIViewController 的视图。我在我的UIViewController 中的前面提到的'UIView. Now the problem I am having is that I need to handle the events of the new 'UIView 中有一个自定义UIView,它包含包含当前'UIView' 的父'UIView'。我该怎么做。

标签: iphone ios uiview uiviewcontroller uinavigationcontroller


【解决方案1】:

将UINavigationController ivar 添加到UIView 并将其分配给主视图控制器。然后您应该可以从UIView 访问它。

编辑:

你的 UIView 子类:

// CustomView.h
@interface CustomView: UIView {
    // ...
    // your variables
    // ...
    UINavigationController *navController;
}

@property (nonatomic, assign) UINavigationController *navController; // assign, because this class is not the owner of the controller

// custom methods

@end

// CustomView.m
@implementation Customview

// synthesize other properties
@synthesize navController;

// implementation of custom methods

// don't release the navigation controller in the dealloc method, your class doesn't own it

@end

然后在[self.view addSubview:showContactFlow]; 行之前添加[showContactFlow setNavController:[self navigationController]];,然后您应该能够从您的UIView 访问您的层次结构的导航控制器并使用它来推送其他UIViewControllers。

【讨论】:

  • 在您的UIView 类中添加一个UINavigationController 成员并将其分配给视图控制器的导航控制器,然后再将视图添加为子视图。然后你就可以访问它了。
【解决方案2】:

您应该尝试使用 MVC 方法。因此,您的控制器可以访问所有这些内容,并且可以继续推送和弹出视图,因此视图不需要对控制器了解太多。

否则,对于这种情况,您可以使用 delegation 快速解决它。所以:

showContactFlow.delegate = self;
[self.view addSubview:showContactFlow];

所以稍后在UIView,你可以说:

[self.delegate addSubview:self];

这是可行的,但它可能不是您应该使用的最佳方法。

【讨论】:

    【解决方案3】:

    单击按钮时,您可以呈现一个视图控制器,例如,

        -(void)buttonFunction{
               ThirdVC *third= [[ThirdVC alloc]initWithNibNme];......
    
               [self presentViewController:third animated:NO];
    
    
        }
    

    使用 Core 动画,您可以在 ThirdVC 的 viewWillAppear: 方法中编写类似动画的 NavigationController 的 pushviewController。

    【讨论】:

    • self 没有找到名为presentViewController的方法
    【解决方案4】:

    你在哪里添加 UIButton 是在 showContactFlow 视图中还是在 ViewController 的视图中??

    关于 modalViewControllers 问题,正确的方法是

        [self presentModalViewController:viewController animated:YES];
    

    向上的标准动画

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多