0
votes

Before I start, I am a beginner, so please don't overwhelm me. (even-though I probably did with you sorry) :/

So basically I have 1 main view (NSView) onto which I am loading other views. So my awakeFromNib method works and it loads my start menu view (main menu to a tic tac toe game). On the menu is a single player and a double player button, and depending on which button I click, I would like the program to load further views.

So I have an AppController class which controls which views are loaded through a setViewController method. The issue is that I have not found a way to connect the buttons from my start menu view to the to the AppController class. So I thought if inside the start menu class I create an object of type AppController and then call on the setViewController method when the single or double player button is pressed, it would change the views accordingly, but it turns out it does nothing. However when I call on the setViewController method inside the AppController class, it does work. So I think the issue has to be somewhere with accessing the view from outside its class, but I might be wrong. Any help would be greatly appreciated, I have spent a lot of time trying to figure this out and I have not had any luck with anything I tried. Here is my AppController class and my start screen class.


AppController.h:

@interface AppController : NSObject

@property (weak) IBOutlet NSView *mainMenu;
@property (strong) NSViewController *mainViewController;

-(void)setViewController:(NSInteger)viewNumber;

@end

AppController.m:

@implementation AppController

@synthesize mainMenu = _mainMenu;
@synthesize mainViewController =_mainViewController;

NSString *const kStartScreen = @"StartScreenViewController";
NSString *const kOnePlayerMenu = @"OnePlayerMenuViewController";
NSString *const kTwoPlayerMenu = @"TwoPlayerMenuViewController";
int test = 0;

enum{
    kStartScreenView = 0,
    kOnePlayerView,
    kTwoPlayerView
};


-(void)awakeFromNib
{
    [self setViewController:0];
}


-(void)setViewController:(NSInteger)viewNumber
{
    [[_mainViewController view] removeFromSuperview];

    if(viewNumber==kStartScreenView)
    {
        self.mainViewController = [[StartScreenViewController alloc] initWithNibName:
                                    kStartScreen bundle:nil];

    }
    else if(viewNumber==kOnePlayerView)
    {
        self.mainViewController = [[OnePlayerMenuViewController alloc] initWithNibName:
                                    kOnePlayerMenu bundle:nil];
    }
    else if(viewNumber==kTwoPlayerView)
    {
        self.mainViewController = [[TwoPlayerMenuViewController alloc] initWithNibName:
                                   kTwoPlayerMenu bundle:nil];
    }

    [_mainMenu addSubview:[_mainViewController view]];
    [[_mainViewController view] setFrame:[_mainMenu bounds]];
}
@end

StartScreenViewController.h:

@interface StartScreenViewController : NSViewController

- (IBAction)OnePlayer:(id)sender;
- (IBAction)TwoPlayer:(id)sender;

@end

StartScreenViewController.m:

@interface StartScreenViewController ()

@end

@implementation StartScreenViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {

    }
    return self;
}

- (IBAction)OnePlayer:(id)sender
{
    AppController *appControllerObj = [[AppController alloc] init];
    [appControllerObj setViewController:1];
}

- (IBAction)TwoPlayer:(id)sender
{
    AppController *appControllerObj = [[AppController alloc] init];
    [appControllerObj setViewController:2];
}


@end
1
Why are you allocing more than one AppController? Seems like there should only ever be one AppController in an app. ;)Ken Aspeslagh
yeah idk, but i solved it, still looking for the answer question button though.. haha just joined this site, but thanks :) and don't I only have 1 appcontroller?jundl77
One will get created by the nib if you have one in there and then you're making a new one each time you call alloc in StartScreenViewController.Ken Aspeslagh
ah ok didnt know that, thanks!jundl77

1 Answers

0
votes

Actually, I figuered it out, all you have to do is make a class method in the AppController which stores the used object (self) as a class variable. Then you can access that object from anywhere. It might not be the most efficient way of doing it, I have no idea as I am just a beginner, but it worked for me! :D

Here's relevant the code:

-(void)awakeFromNib
{
   [self setViewController:kStartScreenView];
   viewObject = self;
}

+(id)getViewObject
{
    return viewObject;
}

viewObject is defined as a class variable of type id.