views:

655

answers:

3

I am not using a tab bar or any other control -- I want to basically change the current view to a different view controller dynamically (i.e. via code). I'm not using Interface Builder at all...

For example, let's say we create three view controllers:

(this may not be the best example, but I'm trying to make it simplistic)

View_Hello.m (and .h)

View_Goodbye.m (and .h)

View_Ciao.m (and .h)

Our ViewerAppDelegate would launch View01_Hello.

View_Hello would have a custom touch method that would then need to go to View_Ciao if swiped, but View_Goodbye if just touched.

Any ideas on how I could do this (and please don't say "oh, you need to use xxx interface element for this example". I need to be able to randomly change views based on programatic control within the view of the application I'm working on)

I'm been surfing through Google and StackOverflow for the past week, and gone through my three O'Reilly Cocoa books (plus three iPhone developer books), and all of them just use simple interfaces -- but nothing shows an example like what I'm trying to do.

===========

Edit (@Andrew Grant):

for example:

View_Ciao *viewCiao; 
-(void) viewDidLoad { 
  viewCiao = [[View_Ciao alloc] initWithNibName:@"View_Ciao" bundle:nil];
  [viewCiao.view setNeedsDisplay];
  [super viewDidLoad];
}

This crashes. :-)

=============

Edit (@Daniel Dickison)

Brilliant -- that worked!

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    // create the second view, remove the first, and then display the second

    viewCiao = [[ViewCiao alloc] init];
    [viewController.view removeFromSuperview];   
    [window addSubview:viewCiao.view];
}
+1  A: 

Load the appropriate view and add it as a subview to the parent, then remove the existing view from its parent.

If you only have one level of views then your window is the parent.

Andrew Grant
I added a comment above to show a way that this would *not* work. Also, do I *have* to have nib files for every class? I'd rather not...
Jeffrey Berthiaume
I don't know why your sample code crashes, but it does not appear to be anything like my suggestion. Also no, you don't have to use nib files.
Andrew Grant
+1  A: 

How about something like:

@interface AppDelegate : ...
{
    View_Hello *hello;
    View_Goodbye *goodbye;
    View_Ciao *ciao;
    UIViewController *currentView;
    UIWindow *window;
}
... IBOutlet properties for the 3 controllers.
@end

@implementation AppDelegate
- (void)switchToGoodbye
{
    [currentView.view removeFromSuperview];
    [window addSubview:goodbye.view];
    currentView = goodbye;
}
... etc.
@end

You may need to resize the view before adding it to the window, and call viewWillDisappear:, viewDidDisappear:, viewWillAppear: and viewDidAppear: on currentView and the new view controller before and after they're removed/added.

Daniel Dickison
+2  A: 

Have you seen recipe 2-8 "Swiping Views" in Erica Sadun's iPhone Developer's Cookbook? She uses touchesBegan and touchesMoved to determine swipe direction, then sets the animation based on the direction in touchesEnded.

You could extend or amend this to add tap detection in order to determine which view to transition to. The recipe is on or around page 69 if you'd like to have a look, anyway. There's also a video (mov) sample here and a code sample here.

NickD