Home / iPad

The viewDidLoad method

Now look at the bolded code section you add in Listing 1-2 marked as View life cycle, which occurs right after the following commented-out code:

/*
  // Implement viewDidLoad to do additional setup after
	    loading the view, typically from a nib.
  - (void)viewDidLoad {
  [super viewDidLoad];
  }
  */

The viewDidLoad message is sent right after the view has been loaded from the nib file, which is the .xib file that you can modify in Interface Builder. This is the place where you insert your code for view initialization, which in this case means displaying the DeepThoughts' falling words.

This would also be the place to insert your code to do anything needed before the view becomes visible. Although I don't use it in this example, you could take advantage of the commented-out loadView statement to create a view hierarchy programmatically, without using a nib file. You could also include a viewWillAppear message, which is sent right before the view will appear. Both viewDidLoad and viewWillAppear are methods declared in the UIViewController class and are invoked at the appropriate times by the framework.

Although I left the commented-out code in place to show where you would insert your version of the viewDidLoad method (right below it), you can delete the commented-out code.

The viewDidLoad method you inserted (in Listing 1-2) starts out by setting up a timer for the interval between each display of falling words:

- (void)viewDidLoad {
    [super viewDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:.5 target:self
	    selector:@selector(onTimer) userInfo:nil
	    repeats:YES];

You use the NSTimer class to create timers. A timer waits until a certain time interval has elapsed and then fires, sending a specified message to a target object. I use the scheduledTimerWithTimeInterval:target:selector: userInfo:repeats: class method to create the timer and schedule it on the current run loop in the default mode. The interval here is 0.5 seconds, the target is self, and the selector is the message to send to the target when the timer fires - in this case, onTimer. The userInfo is the user info for the timer (set to nil), and repeats is set to YES - that is, the timer will repeatedly reschedule itself until invalidated.

Next, the code checks to see whether the kWordsOfWisdom setting has been moved into NSUserDefaults:

if (![[NSUserDefaults standardUserDefaults]
	    objectForKey:kWordsOfWisdom]) {
	[[NSUserDefaults standardUserDefaults]
	    setObject:@"Peace Love Groovy Music"
	    forKey:kWordsOfWisdom];
	fallingWords = @"Peace Love Groovy Music";
  }
  else {
    fallingWords = [[NSUserDefaults standardUserDefaults]
	    stringForKey:kWordsOfWisdom];
  }
  if (![[NSUserDefaults standardUserDefaults]
	    objectForKey:kSpeed] ){
    [[NSUserDefaults standardUserDefaults]setDouble:10.0
	    forKey:kSpeed];
    speed = kMaxSpeed-10.0;}
  else {
    speed = kMaxSpeed-[[NSUserDefaults
	    standardUserDefaults] doubleForKey:kSpeed] ;
  }

The code moves the user's preferences into NSUserDefaults only after the application runs for the first time. However, if you decide to make user preference settings available in the Settings app, Settings will update preferences in NSUserDefaults if the user makes any changes.

If the settings have not been moved into NSUserDefaults yet, the code uses the initial preference value ("Peace Love Groovy Music") for fallingWords.

[[NSUserDefaults standardUserDefaults]setObject:@"Peace
	    Love Groovy Music" forKey:kWordsOfWisdom];
    fallingWords = @"Peace Love Groovy Music";

If the settings have been moved into NSUserDefaults, the code reads them in and then sets fallingWords to whatever the user's preference is.

else {
    fallingWords = [[NSUserDefaults standardUserDefaults]
	    stringForKey:kWordsOfWisdom];

The code then repeats this check with the speed setting. You use standardUserDefaults (a NSUserDefaults class method) to gain access to the standard user default settings.

[Previous] [Contents] [Next]