2010-10-04 109 views
2

首先,我是Objective-C和iPhone編程的新手。有些東西我無法工作。我有一個IPhone窗口應用程序,並在MainWindow中有一個按鈕。當我點擊按鈕時,我想顯示另一個窗口。我已將事件與我的控制器綁定在一起。我只是不知道如何在事件中顯示其他窗口(otherWindow)。如何在IPhone窗口應用程序中顯示另一個窗口

任何人都可以引導來做到這一點?謝謝

回答

1

創建窗口後,請致電makeKeyWindowmakeKeyAndVisible就可以了。

1

正如Apple doc所說,iPhone應用程序通常只有一個窗口。

如果你想顯示另一個'窗口',你可以添加另一個子視圖到1和唯一的窗口。

6

這是添加UIView你的窗口的例子是單擊按鈕時:

- (IBAction)buttonClicked:(id)sender 
{ 
    UIView *newView = [[NewView alloc] initWithFrame:CGRectMake(250, 60, 500, 600)]; 
    [self.view addSubview:newView]; 
    [newView release]; 
} 

這是推動一個新的觀點的例子(假設你使用一個navigationController:

LoginViewController *viewController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil]; 

[self.navigationController pushViewController:viewController animated:YES]; 
[viewController release]; 

這是呈現新視圖(模態窗口)的示例:

FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil]; 

    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentModalViewController:controller animated:YES]; 

    [controller release]; 
+0

上面的翻轉示例中的評論是否應該說「或**推**新視圖」? – JeremyP 2010-10-04 10:43:57

2

我會假設通過「窗口」你實際上意味着一個新的屏幕內容。

第一個問題是:用戶是否應該能夠使用按鈕返回到第一個屏幕?如果是,那麼你應該使用UINavigationController或模態視圖控制器。

UINavigationController爲您提供了一個在屏幕頂部帶有標題和「後退」按鈕的免費導航欄。使用導航模板創建一個新項目以瞭解其工作原理。使用它會簡單:

-(IBAction)didTapButton:(id)sender { 
    CWTheNewController* controller = [[[CWTheNewController alloc] init] autorelease]; 
    [self.navigationController pushViewController:controller animated:YES]; 
} 

導航控制器一樣簡單易用,但新的視圖控制器將覆蓋整個屏幕,它是由你來提供代碼/ UI駁回控制器再次:

-(IBAction)didTapButton:(id)sender { 
    CWTheNewController* controller = [[[CWTheNewController alloc] init] autorelease]; 
    [self presentModalViewController:controller animated:YES]; 
} 

如果用戶應該能夠瀏覽回來,然後,而不是做這樣的事情,以取代完全在屏幕的當前內容:

-(IBAction)didTapButton:(id)sender { 
    CWTheNewController* controller = [[[CWTheNewController alloc] init] autorelease]; 
    self.view.window.rootViewController = controller; 
} 

無論哪種方式,您應該閱讀並理解的第一個文檔是View Controller Programming Guide for iOS一切你用iOS做的應該是使用視圖控制器,否則你做錯了,對你自己來說太難了。

相關問題