开发者

load view controller when application becomes active

I will like my application that always starts as the first time I open i开发者_JAVA百科t. I have several view controllers and when I exit my application and I open it again I see the view controller where I left of. Maybe I have to call the method applicationWillTerminate method.

I use this code to open a new view:

 UIViewController *control = [[SomeViewController alloc] initWithNibName:@"SomeViewController"
                                                                            bundle:nil];
UINavigationController *navControl = [[UINavigationController alloc]
                                      initWithRootViewController:control];

[self presentModalViewController:navControl animated:NO];

[navControl setNavigationBarHidden:YES];

[control release];
[navControl release];

this code works great when linking it to buttons. But when I place that code in the applicationDidBecomeActive method it does not work.


The easiest way is to set UIApplicationExitsOnSuspend in Info.plist.

That really isn't the expected behaviour, though. Users expect to see the app "where they left off", especially if they've only briefly left the app (e.g. because they got a phone call).

Your code snippet adds a view controller, but is unlikely to work since your app delegate is not a UIViewController. It also doesn't do anything about removing the old view controllers.

EDIT: If all you need to do is display a splash screen (or something), then it's something like this:

  • In -applicationDidEnterBackground:, add a "splash screen" view (not a view controller) to self.window. (iOS takes a "screenshot" after you return from -applicationDidEnterBackground: and uses this in the app-switch animation; you want this to be what the user sees when switching back to your app)
  • In -applicationWillEnterForeground:, do whatever animations you want and eventually remove the view from the window (call -removeFromSuperview).

EDIT 2: The same will work in -applicationWillResignActive:/-applicationWillBecomeActive:, except this happens on a sleep/wake event, which might not be what you want...

I'd avoid using view controllers for this, because trying to shoehorn a view controller in the view controller hierarchy is likely to be problematic (for example, you have to figure out which VC to present it from, and you have to do the "right thing" if the user backgrounds your app while the VC is on screen, and...)


The reason it doesn't work in applicationDidBecomeActive is that method is only sent to the Application delegate, which doesn't know about presentModalViewController.

I suggest instead that in your appDelegate, implement applicationWillEnterForeground:, which should restore the state to a newly launched application (equivalent to what the state is at the end of application:didFinishLaunchingWithOptions: ).

OR...(edits) If you just want a certain viewController to run (which is still loaded, right?)...For example, if you have a tab controller and just want to go to the root of the first view controller, put the following code into applicationWillEnterForeground:

UITabBarController * myTabBar = self.tabBarController;
myTabBar.selectedIndex = 0;  
[[myTabBar.viewControllers objectAtIndex:0] popToRootViewControllerAnimated:NO];


Temporary solution:

I made my application crash on applicationWillResignActive method and it works. My application needs to run an animation when I launch it. But this works because next time the application runs it starts like the first time I opened it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜