简体   繁体   中英

iPhone SDK: Advancing from one view to another using a button tap

I want to make a really simple iphone app: one screen with a single button... when the button is tapped a new screen appears. That's it. No animations, nothing,

I've tried endlessly to make the NavBar sample project do this... and it works but only if I use a UINavigationController with a table that I can tap etc. I've tried all the skeleton projects in XCode too.

I thought I was done when I did this:

[[self navigationController] presentModalViewController:myViewController animated:YES];

But I couldn't do it without the UINavigationController. I just want a simple example.

Thanks so much!

One way you could do this is to create a new UIView and then when the button is pressed add that new UIVIew as a subview, therefore making it what you see.

If you make the new view its own subclass of UIView you would do something like this.

LoginView *login = [[LoginView alloc] initWithFrame: rect];
[mainView addSubview: login];
[self presentModalViewController:myViewController animated:NO];

Will pop up a new view, no animations, nothing. To get rid of it, inside myViewController:

[self dismissModalViewControllerAnimated:NO];

Though I reccomend you use the nice sliding animations (change NO to YES.) And yes, you can stack them up. I think this is better than creating a new UIView, but I may be wrong.

The correct way to do this is set up your project with a UINavigationController . In your root view controller, add your button in the view controllers's view. Then in viewDidLoad , register for UIControlEventTouchUpInside events from you button. Then, in your event callback, call:

[self.navigationController pushViewController:[[[SecondViewControllerClass alloc] initWithNib:nibName bundle:nil] autorelease]];

What kdbdallas suggested will work, but you won't get the nice sliding effects, nor will the navigation bar automatically change and provide your users with a back button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM