viewWillAppear not being called

I'm creating an iPhone app starting from "view controller app", following along with the Stanford iPhone class, lecture 6. The app does not have a navigation bar or a tab bar.

I've added the following to the view controller:

- (void)viewDidLoad
{
NSLog( @"view loaded");
[super viewDidLoad];
}

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog( @"viewWillAppear");
}

- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog( @"viewWillDisappear");
}

When the app is first built and run in the simulator both DidLoad and WillAppear are running. When I press the home button WillDisappear does not get called, and when I re-launch the app by touching its icon neither DidLoad nor WillAppear are called. It works for the class instructor, though the SDK and ios have been updated since the class was recorded.

Unrelated question: how do you format code here? (dashes and square brackets not appearing, but are there)

Message was edited by: amyhughes

Message was edited by: amyhughes

Mac Mini, Mac OS X (10.6.4)

Posted on Jul 28, 2010 10:05 AM

Reply
4 replies

Jul 28, 2010 10:20 AM in response to amyhughes

I should add that what I'm trying to do is save some state info and reload it when the app is re-launched, but that seems to be unnecessary. If I omit the save/load (using userdefaults) the state is saved, anyway. It's like the app is suspended and re-loaded as-was without having to do the viewWillAppear stuff. For both this app and the last one in the class the save/reload stuff the instructor is describing is unnecessary.

But I'd still like to figure out how to do the IllAppear/WillDisappear stuff.

Message was edited by: amyhughes

Jul 28, 2010 12:12 PM in response to amyhughes

Hi Amy -
amyhughes wrote:
When the app is first built and run in the simulator both DidLoad and WillAppear are running. When I press the home button WillDisappear does not get called, and when I re-launch the app by touching its icon neither DidLoad nor WillAppear are called. It works for the class instructor, though the SDK and ios have been updated since the class was recorded.

Yes, the difference in behavior is due to the update. You're seeing the difference between iOS 3 and iOS 4. iOS 4 has the ability to keep your app in memory when the home button is pressed so that when you later touch its icon (or return from another activity), the app can return to the foreground in exactly the same state it was in before.

Methods like viewDidLoad, viewDidAppear, viewDidUnload, etc. aren't automatically called when the app switches from active to inactive and vice-versa. Similarly, the state of the app will be preserved during an inactive-active cycle regardless of whether it was saved and retrieved using NSUserDefaults. If you want to take some actions when the app becomes either inactive or active, you can use app delegate methods such as [applicationDidBecomeActive:|http://developer.apple.com/iphone/library/document ation/UIKit/Reference/UIApplicationDelegate Protocol/Reference/Reference.html#//appleref/doc/uid/TP40006786-CH3-SW10].

For more info, see [Clearing value from textfield etc on exit|http://discussions.apple.com/thread.jspa?messageID=11935724&#11935724]. The link provided by Tony Wright in that thread might be especially helpful: [Screw Multitasking: How To Make Your iOS 4 Apps Exit For Real|http://maniacdev.com/2010/07/screw-multi-tasking-how-to-make-your-ios-4-ap ps-exit-for-real>. The article shows how to set a key in your info.plist file which will cause the app to actually terminate when you press the home button. You should then observe the same behavior you would get on iOS 3, as described in your course notes.

Also remember that your app +really will+ terminate when the system shuts down. After a fresh system startup the app won't be restored to any prior session state without using NSUserDefaults or some other scheme involving persistent memory.
Unrelated question: how do you format code here? (dashes and square brackets not appearing, but are there)

See the [yellow alert|http://discussions.apple.com/ann.jspa?annID=749] which is the first topic in this forum. To see how your post will appear, click the preview tab just above the Reply editor panel.

\- Ray

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

viewWillAppear not being called

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.