Hi,
I've found many tutorials on switching views on the iPhone but none that quite match my needs. I wonder if you can help.
I'm looking to have an initial view, A and on a button press unload A and switch to view B.
I've seen the following, but I have reasons why I don't want to do it that way:
Using a navigation controller - Once view A is discarded, I don't need to return to it, so it should disappear completely and be deallocated from memory.
Using a Tab bar controller - same reason as above. I'd like to linearly move from A to B and not B to A - ever.
Using a modal view - actually, the same reason. Also this feels like a bit of a UI no-no, since it gives the user the impression that they're coming back to view A once an action in B is complete.
Please help me, the one thing I'm really struggling with is how to unload the original view controller that's created as part of the app. I think to show the other view all I need to do is init and alloc it in View A, then add it as a subview. However, unloading view A from viewcontroller A is really baffling me.
Thanks in advance!
Edit: On advice from someone below I've implemented view switching, but for some reason removeFromSuperview doesn't work on View A after View B is displayed. Basically I can still see view A behind view B (I made view b slightly smaller so I could test this).
Here's my app delegate:
@implementation View_SwitchAppDelegate
@synthesize window;
@synthesize switchViewController;
@synthesize secondViewController;
@synthesize firstViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
FirstViewController *aFirstView = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
[self setFirstViewController:aFirstView];
[aFirstView release];
[window addSubview:firstViewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[switchViewController release];
[super dealloc];
}
- (void)switchToTwo {
SecondViewController *aSecondView = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
[self setSecondViewController:aSecondView];
[aSecondView release];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
[firstViewController.view removeFromSuperview];
[firstViewController release];
firstViewController = nil;
[self.window addSubview:[secondViewController view]];
[window makeKeyAndVisible];
[UIView commitAnimations];
}
@end
Here's my FirstViewController (You'll see that when the camera picker returns an image, I try to switch views by calling the app delegate method:
#import "FirstViewController.h"
#import "View_SwitchAppDelegate.h"
#import "SecondViewController.h"
@implementation FirstViewController
- (IBAction) takeButtonPressed
{
NSString *type = @"Camera";
[self callCameraWithType:type];
}
- (IBAction) chooseButtonPressed
{
NSString *type = @"Library";
[self callCameraWithType:type];
}
-(void) callCameraWithType:(NSString *)type {
// Set up the image picker controller and add it to the view
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
if ([type isEqual:@"Camera"])
{
NSLog(@"Camera");
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
imagePickerController.sourceType =
UIImagePickerControllerSourceTypeCamera;
}
else {
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
}
else
{
NSLog(@"Library");
imagePickerController.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentModalViewController:imagePickerController animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
//get app delegate
View_SwitchAppDelegate *appDelegate = (View_SwitchAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate switchToTwo];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
// Dismiss the image selection and close the program
[picker dismissModalViewControllerAnimated:YES];
}
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSLog(@"View loaded");
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)removeSelf{
[self.view removeFromSuperview];
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
NSLog(@"First Unload");
}
- (void)dealloc {
NSLog(@"First Dealloc");
[super dealloc];
}
@end