So, here's the issue. I'm relatively new to iOS programming, and I've taken on a giant project.
I'm working on a game with multiple levels which basically follow the same pattern, but have different sprite images and values, and I just decided to lay out all the levels in IB for speed's sake (not necessarily best practices, I know, but work with me). Each "level" has its own view controller, along the lines of "FireLevel1ViewController," "FireLevel2ViewController," etc. All of these view controllers inherit from a custom subclass of UIViewController I created called "GameController."
My problem is, when I open each level on my test device, GameController's viewDidLoad is getting called before the init or viewDidLoad methods of my subclass controllers, and so none of my level images/values are getting assigned to the superclass properties. Specifically, I have a pause menu that ought to be hidden at the outset of the level (I am doing setHidden in GameController's viewDidLoad), but since GameController's viewDidLoad runs before FireLevel1 has a chance to associate the correct IB property with PauseMenu, GameController just hides an empty view, and the actual PauseMenu never gets hidden.
I may have multiple problems going on here, but mostly I think I'm not really understanding correctly how to subclass a subclass of UIViewController and how to get the second subclass's properties/values/images to work in the first subclass's methods.
Thanks so much for any help! I hope that question made sense...
Code for GameController:
@implementation GameController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:pauseMenu];
[pauseMenu addSubview:helpMenu];
//Hides the pause and help menus until their respective buttons are pressed
[pauseMenu setHidden:YES];
[helpMenu setHidden:YES];
isPaused = NO;
}
Code for FireLevel1Controller:
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self)
{
theMainview = mainview;
theScene = scene;
theBG = bg;
theHudView = hud;
thePauseView = pauseMenu;
theHelpView = helpMenu;
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
firstTurret = [[StationaryEnemy alloc]init:turretImage1 baseView:base1];
secondTurret = [[StationaryEnemy alloc]init:turretImage2 baseView:base2];
NSLog(@"I'm in view did load");
}
Did you try using viewWillAppear
? That method should be called after all the visible UI elements have been initialized.
Ahhhh I figured it out - I was accidentally negating my variables. I should've been doing mainview = theMainview; instead of theMainview = mainview - I was just assigning them all to zero. I also moved them all out of init into viewDidLoad, and moved [super viewDidLoad] underneath them, and now it works perfectly!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.