views:

86

answers:

2

FYI I'm programming in objective-C but anyone might be able to help. I've been sitting here for the past two hours trying to figure out what the problem is and I've done everything I know to debug this simple little coding problem. Check out the code below and I will explain. In the application, it starts out with MainScreenViewController. My application gives the user two choices...pick a "master" button or "worker" button (basic client-server model). Based upon the user's choice, the application is then supposed to load another view (either MasterViewController or WorkerViewController). Now, when I run the application, if you have followed the print commands and hypothetically pick the "master" button, then in my program, it prints out "masterButtonPressed - Stage 1" and "masterButtonPressed - Stage 2" but nothing else. The view doesn't change either which tells me the problem lies within the code in the AppDelegate section. After I put the print commands in there and run the application, it still does not print the two statements I have in the AppDelegate section. I do have the correct #import statements in the correct files, I just didn't include them here because that takes up useless space. I can compile the code fine with no error and no warnings. Theoretically, what I should be seeing in the console are four lines when I press the "master" button and they are (in order) - "masterButtonPressed - Stage 1", " changeToMaster - Stage 1", "changeToMaster - Stage 2", and "masterButtonPressed - Stage 2". Can someone point out where I've gone wrong? Like I said the view never changes when press the "master" button. Thanks for your help!

MainScreenViewController.h

@interface MainScreenViewController : UIViewController {

}

-(IBAction)masterButtonPressed;
-(IBAction)workerButtonPressed;
@end

MainScreenViewController.m

@implementation MainScreenViewController
-(IBAction)masterButtonPressed {
    NSLog(@"masterButtonPressed - Stage 1");
    [ErwinAppDelegate changeToMaster];
    NSLog(@"masterButtonPressed - Stage 2");
    }


-(IBAction)workerButtonPressed {
    NSLog(@"workerButtonPressed - Stage 1");
    [ErwinAppDelegate changeToWorker];
    NSLog(@"workerButtonPressed - Stage 2");
}
@end

ErwinAppDelegate.h

@class MainScreenViewController, MasterViewController, WorkerViewController;
@interface ErwinAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    MainScreenViewController *mainScreenViewController;
    MasterViewController *masterViewController;
    WorkerViewController *workerViewController;
}

@property(nonatomic, retain) IBOutlet UIWindow *window;
@property(nonatomic, retain) IBOutlet MainScreenViewController *mainScreenViewController;
@property(nonatomic, retain) IBOutlet MasterViewController *masterViewController;
@property(nonatomic, retain) IBOutlet WorkerViewController *workerViewController;

-(void)changeToMaster;
-(void)changeToWorker;

@end

ErwinAppDelegate.m

@implementation ErwinAppDelegate

@synthesize window, mainScreenViewController, masterViewController, workerViewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after application launch
    [window addSubview:mainScreenViewController.view];
    [window addSubview:masterViewController.view];
    [window addSubview:workerViewController.view];
    [window makeKeyAndVisible];

    // Bring first view up front
    [window bringSubviewToFront:mainScreenViewController.view];
}


-(void)changeToMaster {
    NSLog(@"changeToMaster - Stage 1");
    [window bringSubviewToFront:masterViewController.view];
    NSLog(@"changeToMaster - Stage 2");
}


-(void)changeToWorker {
    NSLog(@"changeToWorker - Stage 1");
    [window bringSubviewToFront:workerViewController.view];
    NSLog(@"changeToWorker - Stage 2");
}
@end
+1  A: 

It looks like you're trying to call a class method: +[ErwinAppDelegate changeToMaster] while you've declared it as an instance method. You'll need to keep a reference back to the app delegate object in your MainViewController - currently you're only dealing with static (class) methods.

The "right" way to do this would be to create a delegate protocol in MainScreenViewController, and after adding that view to your window in ErwinAppDelegate, set mainScreenViewController.delegate = self.

Here's some very useful information on creating and using delegates: How do I create delegates in Objective-C?

pix0r
I'm sorry, you've explained everything great and it all makes sense, but I have one question...which view are you referring to when you say "after adding that view to your window?" Are you talking about after I add the mainScreenViewController view to the window, then declare it as the delegate?
Josh Bradley
What I meant was: after you run `[window addSubview:mainScreenViewController.view]` you'd do something like `mainScreenViewController.delegate = self`. Then inside your MainScreenViewController instance you can call directly to [self.delegate changeToWorker]` etc.
pix0r
A: 

Just a guess, but you probably want an instance of your ErwinAppDelegate to send the message to...

There should be some ErwinAppDelegate eadeleg; [eadeleg changeToMaster]; no?

Dave Gamble
I did try that method of approach, but then it comes up with a warning/error saying that I am referencing an instance variable (in my case it would be window) in a class method, so that didn't quite work out.
Josh Bradley