当应用程序进入前台时查找当前视图。 IOS

时间:2012-09-05 04:47:36

标签: iphone objective-c ios xcode cocoa-touch

当应用程序变为活动状态时,我们如何找到当前第一响应者的视图。我知道应用程序委托applicationWillEnterForeground和applicationDidBecomeActive将被调用,如何使用它来作为第一响应者的亲密视图。

我用google搜索并且堆栈溢出没有找到确切的答案。任何想法的朋友..

1 个答案:

答案 0 :(得分:4)

参考iOS应用程序中最顶层的视图/窗口

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

参考getting-reference-to-the-top-most-view-window-in-ios-application链接。

编辑对应用程序从后台撤回时要执行的当前视图的操作

Add BOOL applicationFromBackground; make its property in appDelegate.

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

现在应用程序进入前台:

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

现在在所有视图中控制器的视图将显示为最顶层的viewcontroller调用的方法,所以这样做:

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

}