Programmatically switch views in Cocoa Touch

How can I programmatically change the appearance of the screen in the application for the iPhone?

I was able to create a navigation view and programmatically push / pull them to produce this behavior, but if I just wanted to change the current view (without using the UINavigation controller object), what is the best way to achieve this?

A simple example: imagine a one-button application that, when clicked, will display a new view or, possibly, one of several views depending on some internal state variable.

I havenโ€™t seen any examples trying to do this yet, and I donโ€™t seem to understand enough about the relationships and initialization procedure between UIViewController / UIView objects to achieve this programmatically.

+6
ios objective-c iphone cocoa-touch
source share
7 answers

I use presentModalViewController:animated: to open the settings view from my main UIViewController window, and then when the user clicks "done" in the settings view, I call dismissModalViewControllerAnimated: from the settings view (referring to the parent view) like this:

 [[self parentViewController] dismissModalViewControllerAnimated:YES]; 
+8
source share

You need to study -[UIView addSubview:] and -[UIView removeFromSuperview] . Your base window is a UIView (child), so you can add and remove it.

+3
source share

How to promote a common UIView in a UINavigationController?

When you want to show one particular view, simply add it as a subtitle to the one that UIView previously clicked. If you want to change the views, delete the previous subheading and add a new one.

0
source share

I found an example program that uses ModalView to display views without inserting them like NavigatorControlView.

iPhone Nav Bar Demo

The link example above uses modalView for an information link. It also uses navigation links for table links.

0
source share

NavBarDemo is interesting, but in the end, modalView is another view that pops and pops out of the stack, from what I can say.

What about cases like the map application, when the map view switches to a list of list view options when the user begins to enter an address? Both of these views are already launched at startup, and only the visibility 0 is specified in the search results table view, and when you enter it, is it switched to 1 or is it really loaded in the touchUpInside event of the text field?

I will try to use the UIView addSubview to recreate this action and post the results

0
source share

You can use the following approach:

BaseView.h - all other views are inherited from this view:

 @interface BaseView : UIView {} @property (nonatomic, assign) UIViewController *parentViewController; @end 

BaseView.m

 @implementation BaseView @synthesize parentViewController; - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { // Initialization code } return self; } - (void)dealloc { [self setParentViewController:nil]; [super dealloc]; } @end 

Other views may inherit from BaseView. Their code should be something like this:

 @implementation FirstView - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { [self setBackgroundColor:[UIColor yellowColor]]; UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)]; [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button1 setBackgroundColor:[UIColor whiteColor]]; [button1 setCenter:CGPointMake(160.0f, 360.0f)]; [button1 setTitle:@"Show Second View" forState:UIControlStateNormal]; [button1 addTarget:self action:@selector(switchView:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button1]; [button1 release]; UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)]; [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button2 setBackgroundColor:[UIColor whiteColor]]; [button2 setCenter:CGPointMake(160.0f, 120.0f)]; [button2 setTitle:@"Show Sub View" forState:UIControlStateNormal]; [button2 addTarget:self action:@selector(showView:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button2]; [button2 release]; } return self; } - (void)showView:(id)sender { if (viewController == nil) { viewController = [[SubViewController alloc] initWithNibName:nil bundle:nil]; } [self addSubview:viewController.view]; } - (void)switchView:(id)sender { SecondView *secondView = [[SecondView alloc] initWithFrame:self.frame]; [self.parentViewController performSelector:@selector(switchView:) withObject:secondView]; [secondView release]; } - (void)dealloc { [viewController release]; [super dealloc]; } @end 

Finally, you will need 1 UIViewController for all views. The switchView: method is called to change the current view. The code for the UIViewController could be something like this:

 @implementation ViewController - (void)loadView { FirstView *firstView = [[FirstView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)]; [firstView setParentViewController:self]; [self setView:firstView]; [firstView release]; } - (void)switchView:(BaseView *)newView { [newView setParentViewController:self]; [self retain]; [self setView:newView]; [self release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)viewDidUnload { [super viewDidUnload]; } - (void)dealloc { [super dealloc]; } @end 

You can download the sample application (within the next hour): http://dl.dropbox.com/u/6487838/ViewSwitch.zip

0
source share

This hint is listed in the Apple documentation , but it does not attach any importance. I found myself trying to figure this out, and it's pretty simple.

Just change the property of the main window of the rootViewController to any view controller that owns the view hierarchy that you want to display. It is so simple. As soon as the property is updated, the contents of the entire window will change to the new contents of the view.

0
source share

All Articles