2010-11-17 44 views
0

我是iOS编程的新手,我已经阅读了一些有关内存释放和分配的文章,并认为我理解这个概念。但在实际的编码过程中,我似乎无法真正应用这个理论。我真的很困惑iOS编程的基础,希望有人能帮忙。提前致谢!分配和释放UI控制器

第一行为 - 我看到的一些应用程序可以在按下iPhone主页按钮时保持当前的Windows状态,以便当下次启动应用程序时,它将显示最后一个状态。 第二行为 - 每次启动时,其他一些应用程序的行为将与其新的启动类似。先前显示的文本,图像等将被清除,并且始终从第一页开始。

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

我已经创建了一个标签栏项目。下面的代码将导致第一个行为。

我已经尝试释放所有在applicationDidEnterBackground的选项卡控制器而不是在dealloc但它没有工作。它仍然会显示最后一个屏幕。

我的问题......

1)如果我叫释放,应该不是销毁窗口呢?例如。 [NavController1发布]。看起来,窗户仍然可以照常工作...

2)我应该改变什么导致第二个行为? 3)如果我分配tabBarController.viewControllers = nil,这是否意味着以前连接到它的页面的内存将被释放?

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


@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]; 
} 

+0

(您可以点击1010按钮将文本格式设置为代码。) – 2010-11-17 15:31:18

回答

0

1)只要保留计数为1个视图控制器会通过调用发布方式发布。

在你的代码中,dealloc永远不会被调用,因此你的视图控制器不会被释放。您需要释放applicationDidEnterBackground中的所有内容...

2)applicationDidEnterBackground这样做:(UIApplication的*)应用

3)这样做:

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

然后在

  • (无效)applicationWillEnterForeground再次重新一切将发布第一页,NavController1和NavController2(以及您可能添加的任何其他视图控制器)

    4)是的,你应该。你不会在这个实例中发生内存泄漏,因为委托从不会被释放,因此仍然有一个对象的有效引用。不,你不能自动释放,因为当调用堆栈返回到运行循环时,autorelease将释放对象。