如何在iPhone窗口应用程序中显示另一个窗口

时间:2010-10-04 01:43:45

标签: iphone objective-c cocoa-touch xcode

首先,我是Objective-C和IPhone编程的新手。我无法开始工作。我有一个iPhone窗口应用程序,在MainWindow我有一个按钮。我想在单击按钮时显示另一个窗口。我将事件与我的控制器绑定在一起。我只是不知道如何在事件中显示我的其他窗口(otherWindow)。

任何人都可以指导这样做吗?谢谢

4 个答案:

答案 0 :(得分: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];

答案 1 :(得分:2)

我会假设通过“window”你实际上意味着一个新的屏幕内容。

第一个问题是:用户是否可以使用按钮返回第一个屏幕?如果是,则应使用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做的一切应该是使用视图控制器,否则你做错了,对自己来说太难了。

答案 2 :(得分:1)

创建窗口后,请在其上调用makeKeyWindowmakeKeyAndVisible

答案 3 :(得分:1)

iPhone应用程序通常只有一个窗口,Apple doc表示。

如果要显示另一个“窗口”,可以将另一个子视图添加到1和唯一窗口。