views:

140

answers:

1

Hello!

I have a problem with switching views in an iPhone application. I have the source code of "Beginning iPhone 3 Development" (http://books.google.com/books?id=TcP2bgESYfgC&printsec=frontcover&dq=beginning+iphone+3+development#v=onepage&q=beginning%20iphone%203%20development&f=false) - chapter 6 - Multiview Applications.

Now I have the problem, I want to create a new view which should switch by clicking the button on the blue screen "Press me". But it did not work.

I add the these lines to the IBAction that the button on the blue screen is pressed:

StartViewController *startController = [[StartViewController alloc] initWithNibName:@"StartViewController" bundle:nil];
self.startViewController = startController;

[self.view insertSubview:startController.view atIndex:1];
[startController release];

But the toolbar at the bottom won't disappear. But I want that this toolbar disappear.

If I wrote

[self.view insertSubview:startController.view atIndex:0];

instead of

[self.view insertSubview:startController.view atIndex:1];

the new xib lies behind the old one, so I see both views, the old and the new. Why? I do not understand this.

Thanks a lot in advance & Best Regards Tim

+1  A: 

The toolbar is in the SwitchView so you would need to hide it from the view if you want it to hide. You could make an IBOutlet for the toolbar and then call setHidden:(BOOL) to hide it. You will need to do this from BlueViewController so you will need a way to get to your super view (which is SwitchView). You will also need to remove the BlueView from the super view by calling removeFromSuperView on blueViewController before inserting the new view into place. It is basically the same code that comes from the switch button in SwitchViewController.

Update: I looked at your code. In BlueViewController.m use this for blueButtonPressed:(id)sender

StartViewController *start = [[StartViewController alloc] initWithNibName:@"StartViewController" bundle:nil];
self.startViewController = start;
[start release];
View_SwitcherAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
SwitchViewController *switchController = appDelegate.switchViewController;
switchController.theToolbar.hidden = YES;
[self.view removeFromSuperview];
[self.view insertSubview:startViewController.view atIndex:0];

You will also need to add these two imports for "View_SwitcherAppDelegate.h" and "SwitchViewController.h".

Scott Densmore
I integrate IBOutlet UIToolbar *theToolbar; and@property(nonatomic,retain) UIToolbar *theToolbar;to SwitchViewController.h and synthesize it.I made connection from "Switch View Controller" in the "MainWindow" to "theToolbar".And with these lines of code: StartViewController *start = [[StartViewController alloc] initWithNibName:@"StartViewController" bundle:nil]; self.startViewController = start; [start release]; [self.view removeFromSuperview]; [self.view insertSubview:startViewController.view atIndex:0]; [theToolbar setHidden:YES];I get the error, that "theToolbar is undeclared".
Tim
Here is my latest source code of the project:ud05_188.ud05.udmedia.de/spotlight/Project.zip
Tim
It doesn't work. The next screen "StartVireController.xib" has a text Label on it. But the screen is white.
Tim
The latest code with your code snippet:http://ud05_188.ud05.udmedia.de/spotlight/View.zip
Tim