I expect the initial view controller of my storyboard to be the current controller - iphone

If the application was previously in the background, when applicationDidBecomeActive is called, I expect the initial view controller of my storyboard to be the current controller.
I used:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self.window makeKeyAndVisible];
}
When I restart app ,loginAciton inside rootViewController still be called ,but could not present the next controller . No errors like nothing happened.
- (IBAction)loginAciton:(id)sender
{
id controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Navigation"];
[self presentModalViewController:controller animated:YES];
}
Why?
PS. My rootViewController is not a UINavigationController.
Thanks for any replies.

A much better way is to add UIApplicationExitsOnSuspend to your Info.plist and set it to YES.

I got it !
if you wanna the initial view controller of your storyboard to be the current controller every time , you can try it :
- (void)applicationDidEnterBackground:(UIApplication *)application
{
exit(EXIT_SUCCESS);
}

You can use the notification too, put this code in applicationDidBecomeActive:
[[NSNotificationCenter defaultCenter] postNotificationName:#"appActivated" object:nil];
and add observer in that current view ...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateView:)
name:#"appActivated"
object:nil];
and don't forget to remove the observer in dealloc: of your current view...
[[NSNotificationCenter defaultCenter] removeObserver:self];
may this will help you..

Related

Dismiss popover when app goes in background

How to dismiss popover when application enters in background?
You can do this using the delegate method in appdelegate.m file
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//put your dissmiss popover code here
}
it is better to register your controller for UIApplicationDidEnterBackgroundNotification or UIApplicationWillResignActiveNotification and dismiss it whenever your app goes to background, this will make your life quite easier i feel.
registering for notification in your viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMethod)
name:UIApplicationDidEnterBackgroundNotification object:nil];
implement the method and
-(void)myMethod{
// dismiss popview here
}
finally un-register from the notification in your view controller
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Send an NSNotification in your app delegate's willResignActive method, and listen for it in your view controller that contains the popup, and have it dismiss said popover when the notification is received.
try this
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//[popover dissmissPopoverAnimated:YES];
}

dismiss a modalviewcontroller when the application enters background

i need to dismiss my uiimagepicker modal viewcontroller automatically when the application enters the background.i tried to put the code to dismissmodalviewcontroller code in viewdiddissappear method,but its not being called.so i made a reference to the viewcontroller in appdelegate and tried to put it in the applicationdidenterbackgroundmethod but still it is not working.can someone point out the right way to do this
Try to add an NSNotificationCenter observer for UIApplicationDidEnterBackgroundNotification in the UIViewController that you want to dismiss. Use the selector to dismiss the modalview
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(didEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver: self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
- (void)didEnterBackground:(NSNotification*)note
{
[self.navigationController dismissModalViewAnimated:NO];
}
Best way to remove the modal when app is moving to background and it works fine .
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(dismissView:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)dismissView:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Also you can remove observer like this
[[NSNotificationCenter defaultCenter] removeObserver: self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
I don't think you need to go through all that.
From the docs:
If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack.
Try calling [self dismissModalViewController:NO] from the parent view controller in your implementation of - (void) viewDidUnload.
This is untested, but the docs imply that it should do the job for you.

How to register for an NSNotification from a UILocalNotification?

I have a tabbar application and let's say that I want to switch to the second tab and popup an alert at 12:00, even if my application is not running.
I got all the code for UILocalNotification working correctly, but then I thought that the best way to do that would be by posting a notification from the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the tab bar controller's view to the window and display.
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
// Handle launching from a notification when the app is NOT running
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
[tabBarController setSelectedIndex:1];
[[NSNotificationCenter defaultCenter] postNotificationName:#"AlertNotification" object:self];
}
return YES;
}
Then, in my SecondViewController.m, I have:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(popUpAlert:) name:#"AlertNotification" object:nil];
}
But this does not work. I suspect that the notification is sent while the viewDidLoad of the SecondViewController has not been called yet, right? Is it possible to work this out? And do you agree on my approach of using NSNotificationCenter in this case?
Thanks in advance.
I quickly created a test project and got it working by putting the notification registration in awakeFromNib (assuming SecondViewController is created in a xib file)
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(popUpAlert:) name:#"AlertNotification" object:nil];
}
I guess, you are right. It doesn't work because you are posting the notification before adding the view controller as observer for it.
Another approach would be to add a bool property to the app delegate to indicate whether the app has been started from the local notification.
The app delegate can be requested from anywhere in the app with [[UIApplication sharedApplication] delegate].
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication shareApplication] registerUserNotificationSettings: settings];

How to refresh UITableView after app comes becomes active again?

I would like my UITableView to reloadData once my app is active again, after a user exits the application. I know I need to implement (in my app delegate):
- (void)applicationDidBecomeActive:(UIApplication *)application
but im not sure how to reference the current UITableView?
UPDATE:
My UITableView is a separate controller. It is actually presented as follows
AppDelegate > Root View Controller > Pushes UITabBarController modally which has a UITableViewController
following up on Ole's answer above
add this when initializing the viewcontroller
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(becomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
add the actual method in the controller
- (void)becomeActive:(NSNotification *)notification {
NSLog(#"becoming active");
}
be sure to clean up the notification
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
If you can't access your view controller from the app delegate, you could also have your controller listen to the UIApplicationDidBecomeActiveNotification notification.
you can create your class called TableViewManager. in there register list of UITableView so that you can refresh any table you want.
it's like this, in yourTableViewManager class, you have a method called
- (void)RefreshTableView:(UITableView *)tableView {
if(tableView != nil)
[tableView reloadData]; }

Activity indicator in a UITabBar application on the iPhone

I have a UITabBar + UINavigationController application which often needs data from the internet. Sometimes it takes quite a while before it gets it, so I would like to show an activity indicator.
What I was trying is to add a activityView to my window in my applicationDidFinishLaunching method:
[window addSubview:tabBarController.view];
fullscreenLoadingView.hidden = YES;
[window addSubview:fullscreenLoadingView];
And then I add the application delegate as a observer to the default notification center:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(startFullscreenLoading:) name:#"startFullscreenLoading" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(stopFullscreenLoading:) name:#"stopFullscreenLoading" object:nil];
and implement the methods:
- (void)startFullscreenLoading:(NSNotification *)notification {
fullscreenLoadingView.hidden = NO;
}
- (void)stopFullscreenLoading:(NSNotification *)notification {
fullscreenLoadingView.hidden = YES;
}
When I then use this directly in the applicationDidFinishLaunching method the loading indicator view shows upp as expected:
[[NSNotificationCenter defaultCenter] postNotificationName:#"startFullscreenLoading" object:self];
But when I use it from one of the navigation controllers the startFullscreenLoading: method is called but I don't see the loading indicator view. Why is that?
Are you loading your data on a background thread? Your main thread will be blocked & won't be able to redraw or process any other events.
The most likely thing I can guess is that your indicator view is below some other view. Try
[[fullScreenLoadingView superView] bringSubviewToFront:fullScreenLoadingView]
If that doesn't work, I would suggest breaking inside of -startFullscreenLoading to make sure the fullscreenLoadingView is still valid.
In my app I did this by adding the UIActivityView in IB and making sure it was above everything else (as a top-level object). It's set to be invisible when stopped.
Then in my code I make it appear with [activityIndicator startAnimating] and make it disappear with [activityIndicator stopAnimating];