0

我想将 UIViewController 类作为子视图添加到现有类中,所以我使用了以下代码。

myEventsView = [[EventNameViewController alloc]initWithNibName:@"EventNameViewController" bundle:nil];
[myEventsView.view setFrame:CGRectMake(0, 39, 320, 400)];
[self.view addSubview:myEventsView.view];

它已成功添加,但问题在于按钮操作,那些必须导航到其他视图控制器的操作不起作用。那些动作方法被调用,但动作没有执行,不明白为什么,请指导上述。

提前致谢。

4

3 回答 3

2

Try

myEventsView = [[EventNameViewController alloc]initWithNibName:@"EventNameViewController" bundle:nil];
[myEventsView.view setFrame:CGRectMake(0, 39, 320, 400)];
[myEventsView willMoveToParentViewController:self];
[self.view addSubview:myEventsView.view];
[self addChildViewController:myEventsView];
[myEventsView didMoveToParentViewController:self];
于 2013-05-02T08:25:18.457 回答
0

文档

您创建的每个自定义视图控制器对象都负责管理单个视图层次结构中的所有视图。

您要做的是将一个视图控制器的视图添加为另一个视图控制器视图的子视图,从而混合两个视图层次结构。这会给你带来你已经经历过的问题。看看 Carbon Emitter 的Abusing UIViewCtrollers文章,它给出了详尽的解释和替代方案。

更新

正如上面正确答案中所建议的那样,有一种方法可以使一个 UIViewController 充当另一个 UIViewController 的容器。从实现容器视图控制器(UIViewController 类参考):

A custom UIViewController subclass can also act as a container view controller. A container view controller manages the presentation of content of other view controllers it owns, also known as its child view controllers. A child’s view can be presented as-is or in conjunction with views owned by the container view controller.

However, this is an iOS >= 5 feature, so my answer will remain correct for applications running iOS versions < 5.

于 2013-05-02T08:07:11.570 回答
-1

Muncken is right. You cannot add myEventsView.view as a subview to self.view, since myEventsView.view is controlled by its own view controller.
What you probably wanted to do is to add just an new view (that is not controlled by another view controller) as a subview to self.view. So, why don't you just instantiate such a view without a new view controller?

于 2013-05-02T08:29:18.647 回答