如何创建一个通用(容器)控制器来接收另一个控制器作为输入

发布于 2024-10-07 06:29:12 字数 1144 浏览 6 评论 0原文

在我创建的应用程序中,有许多页面看起来大部分相同,但某些部分不同。为了处理这种情况,我创建了一个包含子视图的容器控制器。我希望这个子视图由另一个控制器(及其关联的笔尖)的内容填充,我将根据上下文根据需要动态创建该控制器。

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2024-10-14 06:29:12

由于您要在 actionMethod 中释放 contentController
你必须在 init 方法中保留 contentController

- (id)initWithContentController:(UIViewController *)aContentController {
    if ((self = [super initWithNibName:@"MyContainerController" bundle:nil])) {

        contentController = [aContentController retain];
    }
    return self;
}

但是,为什么需要这个?控制器应该控制视图而不是其他控制器。如果您认为您确实需要它,那么您可能想使用 UINavigationController 或 UITabBarController。
您还可以在没有控制器的情况下加载视图(参见此处

我个人认为将 UIViewControllers 放在简单的 UIViewController 中并不是一个更好的方法

希望它有帮助

Since you are releasing contentController in the actionMethod
you have to retain contentController in you init method

- (id)initWithContentController:(UIViewController *)aContentController {
    if ((self = [super initWithNibName:@"MyContainerController" bundle:nil])) {

        contentController = [aContentController retain];
    }
    return self;
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文