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

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #1
    Hi,
    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).

    Code:
    // 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?

    Thanks,
    Nick

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

    Joined:
    Oct 31, 2007
    Location:
    San José
    #2
    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