PDA

View Full Version : Switch View Controller delegate pattern help




pdo100
Sep 24, 2010, 05:52 AM
Hi
I have a problem understanding and implementing delegate pattern to my Switch View Controller.
Its my third day trying to figure out how to call method (swapViews) which is in SwitchViewController from
class MenuViewController. I have read that the best way switching multi views is to use delegate on SwitchViewController, but how do I do it?
Basically, in MenuViewController there is method assign to button which once pressed should call method switch view from MenuViewController to SubstractiveViewController. This swap must be done in SwitchViewController using method swapViews.
I will really appreciate for any help.
Here are my files: SwitchViewController.h / .m and MenuViewController.h / .m

SwitchViewController.h
#import <UIKit/UIKit.h>

@class IntroViewController;
@class MenuViewController;
@class SubstractiveViewController;

@interface SwitchViewController : UIViewController {
IntroViewController *introViewController;
MenuViewController *menuViewController;
SubstractiveViewController *substractiveViewController;
}

@property (retain, nonatomic) IBOutlet IntroViewController *introViewController;
@property (retain, nonatomic) IBOutlet MenuViewController *menuViewController;
@property (retain, nonatomic) IBOutlet SubstractiveViewController *substractiveViewController;

-(void) swapViews;

@end


SwitchViewController.m
#import "SwitchViewController.h"
#import "IntroViewController.h"
#import "MenuViewController.h"
#import "SubstractiveViewController.h"

@implementation SwitchViewController
@synthesize introViewController, menuViewController, substractiveViewController;

- (void)viewDidLoad {
// Initialize intro screen
IntroViewController *introView = [[IntroViewController alloc] initWithNibName:@"IntroView" bundle:nil];
self.introViewController = introView;
self.introViewController.view.tag = 0;

// Initialize menu screen
MenuViewController *menuView = [[MenuViewController alloc] initWithNibName:@"MenuView" bundle:nil];
self.menuViewController = menuView;

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

// Display intro screen for 3 seconds and fade out
// Run menu animation once this one was finished
[UIView beginAnimations:@"intro" context:nil];
[UIView setAnimationDelay:0.5];
[UIView setAnimationDuration:0.5];
self.introViewController.view.alpha = 0;

// To detect end of animation block and run new block delegate must be set up
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(introAnimationDone:finished:context:)];

[UIView commitAnimations];
[introView release];
[menuView release];
[super viewDidLoad];
}

- (void)introAnimationDone:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
self.menuViewController.view.alpha = 0;

// Run menu animation
[UIView beginAnimations:@"menu" context:nil];
[UIView setAnimationDuration:0.5];
self.menuViewController.view.alpha = 1;
[introViewController.view removeFromSuperview];
[self.view insertSubview:menuViewController.view atIndex:0];

[UIView commitAnimations];
}

-(void)swapViews
{
SubstractiveViewController *substractiveView = [[SubstractiveViewController alloc]initWithNibName:@"SubstractiveView" bundle:nil];
self.substractiveViewController = substractiveView;
[menuViewController.view removeFromSuperview];
[self.view insertSubview:introViewController.view atIndex:0];
[substractiveView release];
}

- (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)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[menuViewController release];
[introViewController release];
[substractiveViewController release];
[super dealloc];
}


@end


MenuViewController.h
#import <UIKit/UIKit.h>

@interface MenuViewController : UIViewController {
}

-(IBAction)switchViews:(id)sender;
@end


MenuViewController.m
#import "MenuViewController.h"
#import "SubstractiveViewController.h"

@implementation MenuViewController

-(IBAction)switchViews:(id)sender
{
// Code to call -(void)swapViews method in SwitchViewController
}

- (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)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}


@end

Thanks for any help.



KoolStar
Sep 24, 2010, 06:53 AM
I would recommend taking a look at Apple's Utility Application that is a template in xcode. It essentially is the same thing as you have except that they create the view controller and view in one and then swap to another view via the delegate of the first view controller.

pdo100
Sep 24, 2010, 10:11 PM
I would recommend taking a look at Apple's Utility Application that is a template in xcode. It essentially is the same thing as you have except that they create the view controller and view in one and then swap to another view via the delegate of the first view controller.

Great! Thanks for reply. I got it sorted, although still struggling with understanding, now when I have working code in place I can analyse and make sense out of it.