views:

434

answers:

3

I have a multiple XIB file project. I have a switchViewController class that handles the switching of content views. I want to call the methods in this class from other classes. I have the following code:

//SwitchViewController.h

Code:

#import <UIKit/UIKit.h>
@class MainMenuViewController;
@class GlassRepairsViewController;

@interface SwitchViewController : UIViewController {
 MainMenuViewController *mainMenuViewController;
 GlassRepairsViewController *glassRepairsViewController;

}


@property (retain,nonatomic) MainMenuViewController *mainMenuViewController;
@property (retain,nonatomic) GlassRepairsViewController *glassRepairsViewController;

-(IBAction)goToGlassRepairs;
-(IBAction)goToMainMenu;

@end

//switch view controller m file
Code:

#import "SwitchViewController.h"
#import "MainMenuViewController.h"
#import "GlassRepairsViewController.h"


@implementation SwitchViewController
@synthesize MainMenuViewController;
@synthesize GlassRepairsViewController;

- (void)viewDidLoad {
 MainMenuViewController *mainMenuController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuView" bundle:nil];
 self.mainMenuViewController = mainMenuController;
 [self.view insertSubview:mainMenuController.view atIndex:0];
 [mainMenuController release];
    [super viewDidLoad];
}
//These IBActions are linked to buttons on the view that is linked to this class (swtichViewController)  This works perfectly for showing different nibs

-(IBAction) goToGlassRepairs
{
 if(self.glassRepairsViewController.view.superview == nil)
 {
  if(self.glassRepairsViewController == nil)  
  {
   GlassRepairsViewController *glassRepairsController = [[GlassRepairsViewController alloc] initWithNibName:@"GlassRepairsView" bundle:nil];
   self.GlassRepairsViewController =glassRepairsController;
   [glassRepairsController release];  
  }
  [mainMenuViewController.view removeFromSuperview];
  [self.view insertSubview:glassRepairsViewController.view atIndex:0]; 
 } 
}

-(IBAction) goToMainMenu
{
 if(self.mainMenuViewController.view.superview == nil)
 {
  if(self.mainMenuViewController == nil)  
  {
    MainMenuViewController *mainMenuController = [[ MainMenuViewController alloc] initWithNibName:@"MainMenuView" bundle:nil];
   self.mainMenuViewController = mainMenuController;
   [mainMenuController release];  
  }
  [glassRepairsViewController.view removeFromSuperview];
  [self.view insertSubview:mainMenuViewController.view atIndex:0]; 
 } 

}

//MainMenuViewController.h
Code:

#import <UIKit/UIKit.h>

@class SwitchViewController;


@interface MainMenuViewController : UIViewController {

}

-(IBAction)goToGlass;


@end

//MainMenuViewController.m

Code:

#import "MainMenuViewController.h"
#import "SwitchViewController.h"


@implementation MainMenuViewController


-(IBAction)goToGlass
{
  **//This is where I want to call the goToGlassRepairs method that is inside SwitchViewController's class...**


}

SwitchViewController is the class that handles which nibs to display and remove. MainMenu and glassRepairs are content views. However, MainMenu needs to access the methods in the switchviewcontroller class to allow navigation from the content view.

I've tried

`SwitchViewController *s = (SwitchViewController *)[[UIApplication sharedApplication] delegate];

[s goToGlassRepairs];`

It compiles with no warnings but gives the following errors:

2010-03-09 15:51:28.350 Alfa2Go[29535:207] * -[GoAppDelegate goToGlassRepairs]: unrecognized selector sent to instance 0x3b26620

2010-03-09 15:51:28.362 Go[29535:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*

-[GoAppDelegate goToGlassRepairs]: unrecognized selector sent to instance 0x3b26620'

A: 

Well, my guess would be that [[UIApplication sharedApplication] delegate] is not returning a SwitchViewController, but is instead returning a GoAppDelegate. You need to find your SwitchViewController before you can call methods on it.

Ed Marty
ok, even when I try:switchViewController *s = [[SwitchViewController alloc] init];[s goToGlassRepair];it gives me warnings saying s may not respond to goToGlassRepair
Jonathan
Check spelling? Your first switchViewController is not capitalized and the others are. Also, the code you posted says 'goToGlassRepairs' but what you have there is 'goToGlassRepair'. Make sure the header file is included, of course.
Ed Marty
A: 

You can create a property (id switchController, for instance) in your MainMenu class. As the switch view controller handles the allocation of MainMenu, it can also set this property to himself, so the MainMenu class will have a way to access the controller...

Macmade
A: 

finally got this to work:

what I did was added a function in the app delegate that called the display function in the switchview controller. then i imported the app delegate into my main menu and made a function that called the app delegate's function.

Jonathan