为什么我无法释放添加到窗口的视图的导航控制器

发布于 2024-12-17 05:35:49 字数 916 浏览 0 评论 0原文

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setWindow:[[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]];

    // Override point for customization after application launch.
    //self.window.backgroundColor = [UIColor whiteColor];

    // Create First view
    FirstViewController *firstViewCTL = [[FirstViewController alloc] init];

    // Create UINavigationController
    UINavigationController *navCTL = [[UINavigationController alloc] init];
    [[navCTL navigationBar] setBarStyle:UIBarStyleBlack];
    [navCTL pushViewController:firstViewCTL animated:NO];
    [firstViewCTL release];

    [[self window] addSubview:navCTL.view];
    [navCTL release];
    [[self window] makeKeyAndVisible];
    return YES;
}

我知道添加子视图(addSubview:)将保留要添加的视图。但为什么我现在无法释放拥有已保留视图的导航控制器(navCTL)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setWindow:[[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]];

    // Override point for customization after application launch.
    //self.window.backgroundColor = [UIColor whiteColor];

    // Create First view
    FirstViewController *firstViewCTL = [[FirstViewController alloc] init];

    // Create UINavigationController
    UINavigationController *navCTL = [[UINavigationController alloc] init];
    [[navCTL navigationBar] setBarStyle:UIBarStyleBlack];
    [navCTL pushViewController:firstViewCTL animated:NO];
    [firstViewCTL release];

    [[self window] addSubview:navCTL.view];
    [navCTL release];
    [[self window] makeKeyAndVisible];
    return YES;
}

I understand that adding subview (addSubview:) will retain the view to be aded. But why I can't now release my navigation controller (navCTL) that own the views that has already been retained

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

巴黎盛开的樱花 2024-12-24 05:35:49

-addSubview: 保留视图,而不是视图控制器

您可以使用 UIWindowrootViewController 属性(iOS 4 及更高版本)来保留视图控制器,它还可以节省您自己将视图添加为子视图的麻烦。

...
[window setRootViewController:navCTL];
[navCTL release];
...

-addSubview: retains the view, not the view controller.

You can use UIWindow's rootViewController property (iOS 4 and higher) to retain the view controller, it also saves you adding the view as a subview yourself.

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