天天看點

(iPhone/iPad開發)present某一個ViewController後,試圖添加UINavigationController遇到的一些問題

今天試圖實作這麼一個功能:通過presentViewController呈現某一個viewController,并且在這個頁面中添加一個UINavigationController,可以達到在這個頁面進行逐漸跳轉頁面功能。

最初我是在跳轉後的頁面的initWithNibName等初始化函數添加UINavigationController,然後設定RootViewController為self,這樣是實作了可以從目前界面跳轉到其他界面,但是很快發現一個問題,當在界面中執行dismissViewController時,不會自動執行這個界面的dealloc()函數,也就意味着,記憶體中并沒有釋放之前present的那個界面,那個界面在添加UINavigationController時被retain了,可是幾經嘗試,在那個界面最終還是不容易自動執行dealloc()函數。

最後,我的解決方法是,在需要presentViewController處,添加UINavigationController,并設定rootViewController為需要present的view,present時将navigation那個對象傳過去。具體實作代碼如下:

KYPGRegistRootViewController *viewRegist = [[KYPGRegistRootViewController alloc] initWithNibName:@"KYPGRegistRootViewController" bundle:nil];
    UINavigationController *navRegist = [[UINavigationController alloc] initWithRootViewController:viewRegist];
    [viewRegist release];
    if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
        [self.navigationController presentViewController:navRegist animated:YES completion:nil];
    }else{
        [self.navigationController presentModalViewController:navRegist animated:YES];
    }
    [navRegist release];