回答

2

我想在切換視圖時替換窗口上的根視圖控制器。

例如在我的應用程序中,我先顯示一個加載屏幕,然後將視圖切換到登錄屏幕。

要做到這一點,你需要你的應用程序委託的引用,那麼你可以訪問該窗口屬性和更換根視圖控制器:在appdelgate

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
LoginViewController *loginVC = [[LoginViewController alloc] init];    
appDelegate.window.rootViewController = loginVC; 
+0

intrest,但在ios 5我得到錯誤,當我嘗試使用您的答案的代碼塊的第一行 – 2012-04-25 09:19:25

+0

用您的應用程序委託文件的名稱替換MyAppDelegate。 MyAppDelegate是我的文件的名稱,你的將是不同的 – 2012-04-25 09:25:42

+0

新項目的默認稱爲AppDelegate。我已經更新了我的代碼塊 – 2012-04-25 09:26:48

0

點擊here。這個鏈接是關於如何創建導航控制器你想要的。我在這個鏈接中編寫了代碼。我希望這會對你有所幫助。

+0

我不想在導航控制器中的第一個視圖RootViewController的 – 2012-04-25 09:13:12

1

在你simpleViewController:

- (IBAction) yourButtonAction:(id)sender 
{ 
    UIViewController *Vc = [[theViewControllerYouWantToShow alloc]init]; 
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:Vc]; 
    [self presentModalViewController:nav animated:YES]; 
} 

編輯:

您有三個選項,以顯示您的viewController內容:上面使用presentModalViewController:

    1. 爲例

      將viewController視圖添加爲當前viewController的子視圖。 你的情況:[simpleViewController.view addSubView:nav.view];

    3.or如果你簡單的ViewController是導航根的viewController你可以把其他viewControllers它的導航堆棧。

  • +0

    我不使用什麼presentModelviewController,因爲我需要做一些更多的過程和我的應用程序需要改變viewControllers很多時候 – 2012-04-25 08:49:36

    +0

    好的向我解釋你想要做什麼? – 2012-04-25 09:18:09

    +0

    我第一次在該viewController的視圖呈現簡單的viewController我有三個按鈕點擊按鈕我想加載navigationViewController或TabBarViewController – 2012-04-25 09:22:00

    1

    在appdelegate.h

    @property (strong, nonatomic) id<UIApplicationDelegate>delegate; 
    

    。 m

    @synthesize delegate; 
    

    在我的第一個viewController的.h文件中

    AppDelegate *myappDelegate; 
    -(IBAction)start:(id)sender; 
    
    在我第一次的viewController的.m文件

    -(IBAction)start:(id)sender 
    { 
        NSLog(@"Start Button is clicked"); 
        mvc = [[MasterViewController alloc]initWithNibName:@"MasterViewController" bundle:nil]; 
        myappDelegate = [[UIApplication sharedApplication]delegate]; 
        myappDelegate.navigationController = [[UINavigationController alloc]initWithRootViewController:mvc]; 
        myappDelegate.window.rootViewController = myappDelegate.navigationController; 
        [myappDelegate.window makeKeyAndVisible]; 
    } 
    
    相關問題