Resolved viewDidLoad called in background issue

Discussion in 'iOS Programming' started by nickculbertson, Nov 14, 2011.

  1. nickculbertson, Nov 14, 2011
    Last edited: Nov 14, 2011

    nickculbertson macrumors regular


    Nov 19, 2010
    Nashville, TN
    I have a view based application in which the viewDidLoad method of my initial view controller (FirstViewController) is being called while the other view is being shown. The reason this is an issue is because I am playing music in the viewDidLoad of my first view and it reloads randomly as I'm in my second view (GameViewController).

    // in my FirstViewController.m
    -(IBAction) press1{ 
    	[theAudio stop];
    	GameViewController *views = [[GameViewController alloc] initWithNibName:Nil bundle:nil];
    	[self presentModalViewController:views animated:YES];
    - (void)viewDidLoad {
    	NSString *path = [[NSBundle mainBundle] pathForResource:@"Song1" ofType:@"wav"];
    	theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    	theAudio.delegate = self;
    	[theAudio play];
    I've tried playing the sound on launch from the app delegate but I want to be able to control the AVAudioPlayer variable from my FirstViewController.h since it is playing the first track of a playlist.

    Any thoughts?


    Edit: I put the code in - (void) awakeFromNib { } and that did the trick
  2. North Bronson macrumors 6502

    Oct 31, 2007
    San José
    Why would your FirstViewController randomly be loading its view when it is not preparing to display? Why would something that happens when GameViewController is visible cause FirstViewController to need to load its view?

Share This Page