如何创建一个通用(容器)控制器来接收另一个控制器作为输入
在我创建的应用程序中,有许多页面看起来大部分相同,但某些部分不同。为了处理这种情况,我创建了一个包含子视图的容器控制器。我希望这个子视图由另一个控制器(及其关联的笔尖)的内容填充,我将根据上下文根据需要动态创建该控制器。
我在 MyContainerController.mi 中的某处有以下方法,
- (void) someAction {
UIViewController* contentController = [[MyContentController alloc] init];
UIViewController* containerController = [[MyContainerController alloc] initWithContentController:contentController];
[navigationController pushViewController:pageController animated:YES];
[contentController release];
[containerController release];
}
将控制器保留在属性中
- (id)initWithContentController:(UIViewController *)aContentController {
if ((self = [super initWithNibName:@"MyContainerController" bundle:nil])) {
contentController = aContentController;
}
return self;
}
稍后在 viewDidLoad 中我执行以下操作
- (void)viewDidLoad {
[super viewDidLoad];
[contentViewContainer addSubview:contentController.view];
}
contentViewContainer 是应该保存页面特定信息的视图。 不幸的是,此操作失败并出现 EXC_BAD_ACCESS。 有趣的是,如果我从 viewDidLoad 中分配并初始化内容控制器,一切都会正常。看来我无法通过从其他地方分配的控制器。 任何人都可以帮忙。
In the app im creating there are many pages that look mostly the same with some part which is different. To handle this kind of situation i created a container controller that contains a subview. I want this subview to be filled by the contents of another controller (and its associated nib) which i will created dynamically as needed based on context.
I have the following method somewhere
- (void) someAction {
UIViewController* contentController = [[MyContentController alloc] init];
UIViewController* containerController = [[MyContainerController alloc] initWithContentController:contentController];
[navigationController pushViewController:pageController animated:YES];
[contentController release];
[containerController release];
}
In MyContainerController.m i retain the controller in a property
- (id)initWithContentController:(UIViewController *)aContentController {
if ((self = [super initWithNibName:@"MyContainerController" bundle:nil])) {
contentController = aContentController;
}
return self;
}
Later in viewDidLoad i do the following
- (void)viewDidLoad {
[super viewDidLoad];
[contentViewContainer addSubview:contentController.view];
}
contentViewContainer is the view that's supposed to hold the page specific info.
Unfortunatly this fails with EXC_BAD_ACCESS.
The funny thing is that if i alloc and init the content controller from within viewDidLoad everything works. It seems that i cant pass a contoller i allocated from another place.
Can anyone assist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您要在 actionMethod 中释放 contentController
你必须在 init 方法中保留 contentController
但是,为什么需要这个?控制器应该控制视图而不是其他控制器。如果您认为您确实需要它,那么您可能想使用 UINavigationController 或 UITabBarController。
您还可以在没有控制器的情况下加载视图(参见此处)
我个人认为将 UIViewControllers 放在简单的 UIViewController 中并不是一个更好的方法
希望它有帮助
Since you are releasing contentController in the actionMethod
you have to retain contentController in you init method
But, why do you need this? Controllers are supposed to control views and no other controllers. If you think you really need that then you want to use UINavigationController or UITabBarController maybe.
You can also load views without a controller (see here)
I personally think that having UIViewControllers inside of simple UIViewController is not a preferable approach
Hope it helps