Switch View Controller delegate pattern help

Discussion in 'iOS Programming' started by pdo100, Sep 24, 2010.

  1. macrumors newbie

    Joined:
    Sep 4, 2010
    #1
    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
    PHP:
    #import <UIKit/UIKit.h>

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

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

    @
    property (retainnonatomicIBOutlet IntroViewController *introViewController;
    @
    property (retainnonatomicIBOutlet MenuViewController *menuViewController;
    @
    property (retainnonatomicIBOutlet SubstractiveViewController *substractiveViewController;

    -(
    voidswapViews;

    @
    end

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

    @implementation SwitchViewController
    @synthesize introViewControllermenuViewControllersubstractiveViewController;

    - (
    void)viewDidLoad {
        
    // Initialize intro screen
        
    IntroViewController *introView = [[IntroViewController allocinitWithNibName:@"IntroView" bundle:nil];
        
    self.introViewController introView;
        
    self.introViewController.view.tag 0;
        
        
    // Initialize menu screen
        
    MenuViewController *menuView = [[MenuViewController allocinitWithNibName:@"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
    PHP:
    #import <UIKit/UIKit.h>

    @interface MenuViewController UIViewController {
    }

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

    MenuViewController.m
    PHP:
    #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.
     
  2. macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #2
    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.
     
  3. thread starter macrumors newbie

    Joined:
    Sep 4, 2010
    #3
    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.
     

Share This Page