你說的對,在AppDelegate中創建你的視圖控制器並不好。相反,您應該爲每個視圖創建一個單獨的ViewController。在rootViewController的實現文件中,創建一些方法來推送單獨的視圖控制器,當按下按鈕時,這些控制器要顯示。像這樣的東西可以工作:
- (void)showAboutView
{
AboutViewController *aboutViewController = [[AboutViewController alloc] init];
[self.navigationController pushViewController:aboutViewController animated:YES];
}
- (void)showSettingsView
{
SettingsViewController *settingsViewController = [[SettingsViewController alloc] init];
[self.navigationController pushViewController:settingsViewController animated:YES];
}
然後,將這些方法包含在按鈕的選擇器字段中。像這樣:
UIBarButtonItem *aboutBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showAboutView)];
[[self navigationItem] setRightBarButtonItem:aboutBarButtonItem];
這應該照顧推動你的觀點。 navigationController會自動顯示一個按鈕'返回'。
在AppDelegate中,您將創建的navigationController,並告訴它什麼看法將是RootViewController的,就像這樣:
ViewController1 *vc1 = [[ViewController1 alloc] init];
mainNavigationController = [[UINavigationController alloc] initWithRootViewController:vc1];
希望這有助於!
我會試試這個。希望它能起作用。謝謝! :) – jsanmtosj
嗨。我嘗試了你的建議,並以一些錯誤結束。你能看到我的項目在這裏> http://dl.dropbox.com/u/7539490/WinNav%202.zip並幫助我解決這些錯誤?非常感謝! :D – jsanmtosj
1 - 應用程序didFinishLaunchingWithOptions方法中應包含用於創建navigationController和rootViewController的代碼(當前位於AppDelegate底部,而不是任何方法)。 – superjessi