我一直在爲此奮戰數小時。我到處搜索,似乎無法找到解決我的問題的方法。我很確定我只是缺乏一些關鍵概念。讓modalViewController出現問題
我的AppDelegate(didFinishLaunching)基本建立了我的窗口,並調用RootViewController的:
// create our window UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [window setBackgroundColor:[UIColor blackColor]]; // create our rootviewcontroller RootViewController *controller = [[RootViewController alloc] init]; // add our rootviewcontroller's view to our window [window addSubview:controller.view]; // controller is now owned by window's view [controller release]; // show us to the world [window makeKeyAndVisible];
當我添加controller.view作爲窗口的子視圖,我的理解是,RootVC的的loadView就會自動被調用。
在RootVC loadView中,我創建了一個tabBarController,每個選項卡都有一個navigationController和它自己的viewController。所有這一切工作正常。
在RootVC viewDidLoad中,我正在檢查這是否是用戶第一次運行此應用程序,如果是,我想拋出一個模式歡迎屏幕。這是我遇到麻煩的部分。
我想保持儘可能多的代碼了RootVC的viewDidLoad方法中,最好將能夠完成我想要的這個:
WelcomeViewController *welcome = [[WelcomeViewController alloc] init]; [self presentModalViewController:welcome animated:true]; [welcome release];
顯然,這是行不通的。 WelcomeVC的loadView還沒有運行,因爲我沒有明確地設置它的視圖屬性。我玩過很多不同的解決方案(welcome.view - [[UIView ....],使用WelcomeVC的init方法設置self.view),但我似乎無法獲得該模式彈出。
我應該怎麼做才能找到我想要的?什麼是最佳實踐,以及保持我的代碼緊湊和整潔的最佳解決方案是什麼?
我被卡住了,所以希望您的解決方案能讓我繼續開發我的應用程序!
與你的問題沒有關係,我不認爲[window addSubview:controller.view]會保留視圖控制器。 – MHC 2011-02-18 05:22:07
@MHC - 你能詳細說明一下嗎?爲什麼你這麼想?我認爲每當一個視圖添加一個子視圖時,該子視圖就會被保留。 – djibouti33 2011-02-18 05:44:23