分配和释放UI控制器

时间:2010-11-17 15:25:22

标签: iphone ipad ios

我是iOS编程的新手,我已经阅读了一些关于内存发布和分配的有用文章,并认为我理解这个概念。但在实际编码过程中,我似乎无法真正应用该理论。我真的很困惑iOS编程的基础知识,希望有人可以提供帮助。提前谢谢!

第一种行为 - 我看到的一些应用程序可以在按下iPhone主页按钮时保持当前的窗口状态,这样当下次启动应用程序时,它将显示它所处的最后状态。 第二种行为 - 其他一些应用程序每次启动时都会像新的启动一样运行。以前显示的文本,图像......等将被清除,它将始终从第一页开始。

我想做的就像第二种行为 - 按下主页按钮时清除所有内容,并在每次启动时重新开始。

我创建了一个标签栏项目。以下代码将导致第一种行为。

我尝试在applicationDidEnterBackground释放所有选项卡控制器,而不是dealloc,但它不起作用。它仍会显示最后一个屏幕。

我的问题......

1)如果我打电话给释放,它也不应该破坏窗户吗?例如。 [NavController1发布]。看起来窗口仍然可以照常工作......

2)我应该改变什么导致第二种行为?

3)如果我指定tabBarController.viewControllers = nil,是否意味着之前附加到它的页面的内存将被释放?

4)我是否需要发布IBOutlets变量?例如。 UIWindow *窗口,UITabBarController,UITextField等。即使我没有释放它,它似乎也没有给我内存泄漏。原因可能是因为我不知道如何杀死应用程序。我试图双击iphone主页按钮来结束应用程序,但我仍然无法让调试达到dealloc功能。一篇文章说,如果你分配或保留它,你应该释放它。既然我没有在这里分配任何东西,我可以不发布吗?或者是否可以设置自动释放?


@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
    UIWindow *window;
    UITabBarController *tabBarController;
    UINavigationController *NavController1;
    UINavigationController *NavController2;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.
    NavController1 = [[UINavigationController alloc] init];
    NavController2 = [[UINavigationController alloc] init];

    //This view controller inherits UIViewController and has a xib
    FirstViewController *firstpage = [[FirstViewController alloc] init];
    firstpage.title = @"First Page";

    //These view controllers inherits UIViewController and delegate of UINavigationControllerDelegate and has a xib
    SecondViewController *secondpage = [[SecondViewController alloc] init];
    secondpage.title = @"Second Page";
    [NavController1 pushViewController:secondpage animated:YES];
    [secondpage release];

    ThirdViewController *thirdpage = [[ThirdViewController alloc] init];
    thirdpage.title = @"Third Page";
    [NavController2 pushViewController:thirdpage animated:YES];
    [thirdpage release];

    // Add them as children of the tab bar controller
    tabBarController.viewControllers = [NSArray arrayWithObjects:firstpage, NavController1, NavController2, nil];

    // Don't forget memory management
    [firstpage release];

    // Add the tab bar controller's view to the window and display.
    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];

    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     */
}

- (void)dealloc {
    [NavController1 release];
    [NavController2 release];
    [tabBarController release];
    [window release];
    [super dealloc];
}

1 个答案:

答案 0 :(得分:0)

1)只要保留计数为1,您的视图控制器将通过调用release来释放。

在您的代码中,永远不会调用dealloc,因此无论如何都不会释放视图控制器。您需要在applicationDidEnterBackground中发布所有内容......

2)在applicationDidEnterBackground中执行以下操作:

[NavController1 release]; 
NavController1 = nil;
[NavController2 release];
NavController2 = nil;
tabBarController.viewControllers = nil;

然后再次在

中重新创建所有内容
  • (void)applicationWillEnterForeground:(UIApplication *)application

3)这样做会释放firstPage,NavController1和NavController2(以及你可能添加的任何其他视图控制器)

4)是的,你应该。在这种情况下,您不会收到内存泄漏,因为委托永远不会被dealloc'd,因此仍然存在对对象的有效引用。不,你不能自动释放,因为当调用堆栈返回到运行循环时,autorelease将释放该对象。