Determine if UIViewController is closing due to application exit? - iphone

Is there a way to test in viewWillDisappear if it's being called because the application is exiting, versus normal ways of it being dismissed? The method applicationWillTerminate in the App Delegate is called after the current view is closed. I want to do different things depending on whether it's being dismissed due to a IBAction or the user clicking the menu button.
Thanks!

You should use observe the UIApplicationWillTerminateNotification in your controller, set a flag, and then check for the flag in your viewWillDisappear implementation.
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:#selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:nil];

I haven't used it for your purposes yet, but the UIApplicationWillResignActiveNotification notification might occur before applicationWillTerminate is called.
Just throw...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillResign:) name:UIApplicationWillResignActiveNotification object:NULL];
... into your UIViewController to test it out.

Related

NSNotificationCenter one post causes observers to be called twice

I have the following code:
[[NSNotificationCenter defaultCenter] postNotificationName:kNewsfeedFetchCompleted object:self userInfo:userinfo];
only this, no where else. And here's how I set the observer:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newsfeedFetchCompleted:) name:kNewsfeedFetchCompleted object:nil];
question is when I do one post the newsfeedFetchCompleted is called twice.. how is this even possible?
This is possible when your code for addObserver is executed twice. The notification function will be called as many times as it is registered.
So make sure your code for adding observer is executed for once only. So, you can keep it in viewDidLoad or init method.
If you are putting it in viewWillAppear then remove observer in viewWillDisAppear.
before you add observer, make sure you remove the previous observer added.
[[NSNotificationCenter defaultCenter]removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newsfeedFetchCompleted:) name:kNewsfeedFetchCompleted object:nil];
It is possible if you have added the same observer multiple times for the newsfeedFetchCompleted notification. You should match your addObserver calls with removeObserver calls.
For example if you added the observer in viewWillAppear/viewWillDidAppear/ViewDidLoad of a UIViewController, you should remove it in viewWillDisappear/viewDidDisappear/ViewDidUnload.
The corresponding remove call for addObserver, is removeObserver:name:object:
More info can be found in the NSNotificationCenter docs

How can a viewController know when an app did just finish launching?

I've made a simple single view program in xcode 4.3.1. I'd like the view to do different things depending on if it is being loaded the first time the application starts vs when it is being resumed.
Can anyone tell me the best way to do this?
The appDelegate has no reference to my viewController so I'm not sure I can pass a variable from my AppDelegate didFinishLaunchingWithOptions method.
How does the AppDelegate communicate with the ViewController when the ViewController does not seem to be instantiated anywhere?
Thanks!
You can use NSNotificationCenter to know when the application enters the foreground, and then you can register individual VCs to care about the event. For example:
- (void)loadView {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
The VC registers during loadView (or any other method). Then when the app enters the foreground, the method
- (void)applicationWillEnterForeground;
is called. Just remember to unregister in dealloc or viewDidUnload.
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
You can use [application:didFinishLaunchingWithOptions:] to determine if you just started. It is only called once when you launch. You can combine that with setting some flags and
[applicationWillEnterForeground:(UIApplication *)application] to determine if you launched or are simply returning to the foreground.

NSNotificationCenter in viewDidLoad: not working

for some reason this code isn't working in viewDidLoad, but will work in viewWillAppear. Any ideas?
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(wakeUp:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
Thank you
you're definitely sure viewDidLoad is being invoked?
For my case is that I put the removeObserver method inside didReceiveMemoryWarning method, and if I take a picture or do something else, this method fires out of my expect. So, now I always remove notification observer at dealloc stage.

Reloading TableView on iPhone from another class

So I have a FirstViewController, which is a UITableViewController, as well as delegate and dataSource. It has the table view.
I have another class, FeedParser, that parses the data - but after it's done parsing, I need it to go and refresh the UITableView or else it won't show anything.
This is probably a stupid question, so forgive me, but how should I go about calling FirstViewController's tableView.reloadData from FeedParser?
Is there a method to return that view?
Thanks!
Register the view controller to receive notifications that the data has been changed, and have it refresh the table when it receives one. Then have the parser send it out.
Registering for it is easy:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(yourRefreshMethodHere:)
name:#"YourNotificationName"
object:nil];
Your refresh method needs to be set up to receive these notifications, along these lines:
- (void)reloadTable:(NSNotification *)notif {
[self.yourTableName reloadData];
}
And it's important to stop observing in your ViewDidUnload:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Then in the parser you need to simply add this when it's complete:
[[NSNotificationCenter defaultCenter] postNotificationName:#"YourNotificationName"
object:nil];
The view controller (and anyone else observing the notification with that name) will get the message and perform its task.
Simple method,
Create a object for your FirstViewController in appDelegate, assign property and synthesize it.
In ViewDidLoad of FirstViewController,
firstViewControllerObj = self;
In feedParser.m, after the parsing done code as follows,
[appDelegate.firstViewControllerObj.tabelView reloadData];
Just store a reference to FirstViewController on your app delegate, then call [appDelegate.firstViewController.tableView reloadData].

App Delegate - Unload View Controller

I'm trying to unload a view controller from view when the iPhone goes to sleep. My app has a stopwatch that has to keep counting when the phone goes to sleep or call comes in or the user closes the app without logging out.
I have all this functionality in place, I'm capturing all start times and stop times and upon re-entering the stopwatch view controller, I calculate the difference. It all works beautifully. When I was doing some additional testing I realised I hadn't catered for the iPhone going into sleep mode.
So all I need to do to make sure my stopwatch is correct bring the user back to the app home screen. I know the following method is called when the app goes to sleep:
-(void)applicationWillResignActive:(UIApplication *)application
How do I unload the stopwatch view controller from my app delegate ?
---- UPDATE ----
kpower, thanks for your feedback. I've implemented the following code:
In my App Delegate:
- (void)applicationWillResignActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"AppIsAsleep" object:nil];
}
In my view controller, I have the following:
-(void)viewDidLoad
{
// Add Observer.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(viewDidUnload:) name:#"AppIsAsleep" object:nil];
}
- (void)viewDidUnload {
//Remove the Observer.
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"AppIsAsleep" object:nil];
}
When the phone goes to sleep, it actually closes the app, am I doing something wrong ?
Regards,
Stephen
You can use the Notifications mechanism. It allows you to unload view controller from different place (not the AppDelegate) this case.
For example, in your view controller's viewDidLoad method you add an observer (don't forget to remove it in viewDidUnload) and in applicationWillResignActive: method of AppDelegate you just simply post notification. That's all.
↓ Update here ↓
When you get a notification - you should manage view controller's removing by yourself. And calling viewDidUnload here is not the solution, cause this method is called after view controller was already unloaded and doesn't cause removing.
How to remove? Depends on how the view controller was added (for example, popViewControllerAnimated for UINavigationController). The main idea here is to make object's retain count equal to 0 (as you know this case an object will be destroyed) - so you should sent release message necessary amount of times.