2010-11-17 77 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將釋放對象。