How to reload view when application becomes active after suspended? - iphone

When the user changes NsuserDefaults in settings on the iPhone and activates my app after it being suspended, i want to reload the active view.
How can i do this?

You could implement the applicationDidBecomeActive: method in your application delegate, or register for the UIApplicationDidBecomeActiveNotification notification in any other object.

- (void)applicationWillEnterForeground:(UIApplication*)application on the App Delegate is invoked when coming out of suspend.
Implement this method, in it check if the setting you are about has changed, find your topmost view controller and reload it's view then.

Related

How can be used phone locks as a action trigger in Swift?

I want to change to another viewcontroller in swift when the phone is locked. Is there any variable or function?
When the user locks their phone 'applicationWillResignActive' and then 'applicationDidEnterBackground' from the App Delegate get called and then when the user unlock it - 'applicationWillEnterForeground' and 'applicationDidBecomeActive' get called. You can either implement your view controller to be pushed in the App Delegate or add a notification observer in your view controller to react to any of these and push/pop/modally present the view controller you need. It all depends on your implementation - it is a bit of a broad question. Hope this helps!

How to start the first view every time when app resumes?

I am using xcode4.2 and inside appDidFinishLaunching i am initializing navigationController with my first view and sets the rootViewController of app to navigationController.I want to show the first view every time when app resumes. How i can i do this?
The easiest way I can think of is not using backgrounding. If you set the UIApplicationExitsOnSuspend key in Info.plist, when the user press the home button, your app will quit and next time it is launched it will start over and not resumed from where it was.
Otherwise, you can define – applicationWillEnterForeground: and – applicationDidBecomeActive: to go back to your first view whenever the app is resumed. Have a look at the UIApplicationDelegate reference and Multitasking states.

View Controller lifecycle

Hi I have a resetView method that resets my view too a default state and that method gets called on loadView and when the device is shaken , the two view controllers are in a tab bar controller so when I change tabs the viewWillAppear method is call and resets the view but when close the application and it go's into the background the next time it is relaunched the view wont reset I have add the reset view method call in to
-(void)viewWillAppear:(BOOL)animated;
-(void)viewDidAppear:(BOOL)animated ;
- (void)viewDidLoad;
-(void)awakeFromNib;
but none of them are being called when the application is woken from the background
Look at the application delegate methods. You should call any methods from when the app is started from there. If your view was the last view when you left the app, none of the methods you listed should be called. Read the documentation about when they are called.

Application comes to foreground from background

When application comes to foreground from background then appixationDidBecomeActive gets called. Now, I want to refresh the page from which the home button was tapped. In which method I can write the refresh code. viewWillAppear is not getting called.
In this case viewWillAppear never gets called, you can fire notification(s) whenever -(void)applicationWillEnterForeground:(UIApplication *)application gets called and use it on the view you want.
You can use custom notification like : NSNotificationCenter.
And on call of this you can refresh your view.

viewDidAppear not firing ever again after app enters foreground

I have traced a problem in my iPhone app code to the viewDidAppear method not always firing. When you start the app the event fires as expected. However if I close the app using a phone capable of multitasking and reopen in. My viewDidAppear events no longer fire.
My views are loaded from Nibs and I use viewDidUnload to clean up (release and nil all outlets). My views are nested in side and tab bar then navigation controllers. I looks like the events aren't wired up properly when the nibs reload. Any idea on what I'm doing wrong/missing and how I can fix this?
Thanks in advance.
UPDATE I do not mean the event is not fired when the app first comes into the foreground. I mean the event never fires again. Even when changing between tabs or moving though the navigation views.
Example:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"viewDidAppear called");
}
This code is placed in two views, each on different tabs. Each time I swap between tabs "viewDidAppear called" is written to the log. When I close and reopen the app and swap between tabs this no longer happens. Other button events fire normally.
Btw, the viewDidUnload method is really badly named btw -- it's not an 'opposite' to viewDidLoad, it's only called if there was a low memory situation and the view for that controller was unloaded due to not being visible at that time.
(ORIGINAL, NOT SO RELEVANT ANSWER:)
Please see my answer to this similar question:
Why does viewWillAppear not get called when an app comes back from the background?
Basically, viewDidAppear gets called after your UIViewController's view was added to the application's UIWindow heirarchy. Backgrounding then restoring the app doesn't change your view in that respect, so viewDidAppear doesn't get called -- it's correct behaviour, and not a bug. Check out the API docs for UIViewController.
Found it.
While not new to programming I am new to iPhone development. On researching this problem I found it was not recommended to call the viewWillAppear and viewWillDisappear methods manually.
My viewWillDisappear methods resign any keyboards if shown, when my app enters the background it loads a splash screen ready for when the app re-enters the foreground (there is some logic I need to do to work out what the user is shown on restarting the app and I can do this under the splash screen).
As viewWillDisappear is not called when the app goes into the background to make sure no keyboards appeared over my splash screen I was calling viewWillDisapper in the applicationDidEnterBackground method. I guess this also un-registers my events.
By adding viewWillAppear to my applicationDidEnterForeground method my events started firing again. Lesson learned, I will refactor this so I don't call these events manually.
Thanks for the help.