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