2

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..

Newbee
  • 3,231
  • 7
  • 42
  • 74
  • What do you mean by "which view is presently showing"? You could have many views visible at any given moment. Do you mean which view is the current first responder to events? Or something else. Please clarify your question. – user1118321 Sep 05 '12 at 04:54
  • @user1118321 You are right, I am talking about view which receives user interaction, first responder. – Newbee Sep 05 '12 at 05:01

1 Answers1

4

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.
  }

}
Community
  • 1
  • 1
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • 1
    refer the link i provided in my answer as it will resolve all your questions – Paresh Navadiya Sep 05 '12 at 05:22
  • Prince - I checked your link thanks, it was helpful, one last question. Using this how do I inform all my views. If my question is wrong sorry for that. – Newbee Sep 05 '12 at 05:27
  • i don't understand what exactly is your requirement? – Paresh Navadiya Sep 05 '12 at 05:31
  • When application enters foreground, I want to inform a particular view(if it is top view) and do some action in that view. I will come to know application enter foreground from application delegate, what to do next.. thats my question.. hope clear. – Newbee Sep 05 '12 at 05:36
  • Thank you so much for your patience and your valuable answer. – Newbee Sep 05 '12 at 05:51