Finding the current view when application enter foreground. IOS

匆匆过客 提交于 2020-01-01 19:42:13

问题


How do we find which view is presently first responder when application become active. I know application delegates applicationWillEnterForeground and applicationDidBecomeActive will be called in cases, how can I use this to intimate view which is first responder.

I have googled and stack overflowed didn't find exact answer. Any idea friends..


回答1:


Getting reference to the top-most view/window in iOS application

topMostView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];

Refer getting-reference-to-the-top-most-view-window-in-ios-application link.

EDIT intiate action to current view to perform when application returing from background

Add BOOL applicationFromBackground; make its property in appDelegate.

Intially it will be applicationFromBackground = FALSE; in application didFinishLaunchingWithOptions method;

Now application enters foreground :

- (void)applicationDidEnterBackground:(UIApplication *)application
{
   applicationFromBackground = TRUE;
}

Now in all view controllers's view will appear method which will be called for topmost viewcontroller so do this:

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  // Create AppDelegate instance
  AppDelegate *objAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  if(objAppDelegate.applicationFromBackground)
  {
     applicationFromBackground = FALSE;
     //do what u want.
  }

}


来源:https://stackoverflow.com/questions/12274627/finding-the-current-view-when-application-enter-foreground-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!