First of all, I'm new to Objective-C and IPhone programming. There is somehing I can not get开发者_如何学Go to work. I have a IPhone window application and in the MainWindow I have a button. I want to show another window when I click the button. I've bind the event with my controller. I just don't know how to show my other window (otherWindow) in the event.
Anyone can guide to do this ? Thank you
This is an example of adding a UIView
to your Window when a button is clicked:
- (IBAction)buttonClicked:(id)sender
{
UIView *newView = [[NewView alloc] initWithFrame:CGRectMake(250, 60, 500, 600)];
[self.view addSubview:newView];
[newView release];
}
This is an example of pushing a new view (assuming you're using a navigationController:
LoginViewController *viewController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
And this is an example of presenting a new view (modal window):
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
I will assume that by "window" you actually mean a new screenful of contents.
First question is: should the user be able to return to the first screen with the button? If yes then you should use a UINavigationController
, or a modal view controller.
The UINavigationController
gives you a navigation bar with a title and "back"-button for free at the top of the screen. Create a new project using the Navigation Template to learn how it works. Using it would be as simple as:
-(IBAction)didTapButton:(id)sender {
CWTheNewController* controller = [[[CWTheNewController alloc] init] autorelease];
[self.navigationController pushViewController:controller animated:YES];
}
The navigation controller is just as simple to use, but the new view controller will cover the whole screen, and it is up to you to provide code/UI to dismiss the controller again:
-(IBAction)didTapButton:(id)sender {
CWTheNewController* controller = [[[CWTheNewController alloc] init] autorelease];
[self presentModalViewController:controller animated:YES];
}
If the user should not be able to navigate back, then instead do something like this to replace the current contents of the screen completely:
-(IBAction)didTapButton:(id)sender {
CWTheNewController* controller = [[[CWTheNewController alloc] init] autorelease];
self.view.window.rootViewController = controller;
}
Either way the first document you should read up and understand is View Controller Programming Guide for iOS. EVERYTHING you do with iOS should be using view controllers, otherwise you are doing it wrong, and extra hard for yourself.
After creating your window, call makeKeyWindow
or makeKeyAndVisible
on it.
iPhone apps typically have only 1 window, as the Apple doc says.
If you want to show another 'window', you can add another subview to the 1 and only window.
精彩评论