【问题标题】:Switching the view after applicationDidBecomeActive在 applicationDidBecomeActive 之后切换视图
【发布时间】:2023-04-06 11:30:02
【问题描述】:

我目前正在开发一个应用程序,该应用程序需要在后台运行超过五分钟后返回到另一个视图。为了做到这一点,我必须在按下主页按钮后在后台运行一个计时器,或者在短信或电话等中断的情况下,然后,五分钟后,应用程序将需要转到另一个视图。我知道必须使用 applicationDidBecomeActive 方法,但是如何使用呢?我也知道可以在 applicationDidBecomeActive 中刷新视图,但这是如何完成的? (我没有使用故事板。)

【问题讨论】:

    标签: ios


    【解决方案1】:

    实际上,您应该使用UIAppDelegateapplicationDidEnterBackground applicationWillEnterForeground 委托方法或通过注册到适当的系统通知来执行此操作(didBecomeActive 在其他场合也被调用,例如当UIAlertView 被调用时从屏幕上消失)。

    这应该是(可能包括语法问题,我在这里是文本框编码):

    • 在视图控制器的viewDidLoad 方法中,注册到通知:

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];

    • 实现willEnterForeground:didEnterBackground: 方法。在willEnterForeground: 中,使用CACurrentMediaTime()[NSDate date] 对当前时间进行采样。在didEnterBackground: 中再次采样时间并计算时间差。由于此方法是在视图控制器内部实现的,因此您可以随意操作 self.view 的子视图。

    • 不要忘记删除 dealloc 方法中的观察者(viewDidUnload 自 iOS 6.0 起已弃用,因此请注意):

      [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil] [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]

    【讨论】:

      【解决方案2】:

      您可以这样做。我刚刚制作了一个测试应用程序,我确认它运行良好。代码:

      #import "AppDelegate.h"
      #import "ViewController.h"
      #import "theView.h"
      
      NSTimer *theTimer;
      UIViewController *theViewController;
      BOOL theTimerFired = NO;
      
      @implementation AppDelegate
      
      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
          self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
          self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
          self.window.rootViewController = self.viewController;
          [self.window makeKeyAndVisible];
          return YES;
      }
      
      - (void)applicationWillResignActive:(UIApplication *)application
      {   
          // Set a 5 minute timer (300 seconds)
          theTimer = [NSTimer scheduledTimerWithTimeInterval:300.0 target:self selector:@selector(presentVC) userInfo:nil repeats:NO];
      }
      
      - (void)presentVC
      {
         // Set a boolean to indicate the timer did fire
         theTimerFired = YES;
      }
      
      - (void)applicationDidBecomeActive:(UIApplication *)application
      {    
          // Check to see if the timer did fire using the previous boolean we created
          if (theTimerFired == YES)
          {
              theViewController = [[UIViewController alloc]initWithNibName:@"theView" bundle:nil];
      
              [self.viewController presentViewController:theViewController animated:YES completion:NULL];
      
              [theTimer invalidate];
          }
      }
      
      @end
      

      【讨论】:

      • 因为在此处保留计时器的唯一原因是在您回到前台后测量时间(例如,而不是在后台对任务进行计时),因此最好对两个事件的时间,而不是使用计时器。另外,请参阅this,了解有关后台NSTimers 的更多信息。
      • 非常感谢troop231。如果我希望视图是不同的视图怎么办? (我很抱歉展示了我的新手。
      • 它可以是您想要的任何视图。只需确保在 AppDelegate 中导入视图的标题。您可以将theViewController = [[UIViewController alloc]initWithNibName:@"theView" bundle:nil]; 替换为您想要的任何内容。
      • 你能给我举个例子吗?我试过这样做,但它什么也没做。
      • FirstScreen = [[UIViewController alloc]initWithNibName:@"PLCFirstScreenView" bundle:nil]; [self.firstscreen presentViewController:FirstScreen Animation:YES completion:NULL];
      猜你喜欢
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      相关资源
      最近更新 更多