Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在应用程序后切换视图IDBECOMEACTIVE_Ios - Fatal编程技术网

Ios 在应用程序后切换视图IDBECOMEACTIVE

Ios 在应用程序后切换视图IDBECOMEACTIVE,ios,Ios,我目前正在开发一个应用程序,在后台运行超过五分钟后需要返回到另一个视图。为了做到这一点,我必须有一个定时器在按下Home(主页)按钮后在后台运行,或者在出现短信或电话中断的情况下,五分钟后,应用程序将需要转到另一个视图。我知道必须使用ApplicationIDBecomeactive方法,但如何使用?我还知道可以在ApplicationIDBecMeactive中刷新视图,但如何做到这一点?(我不使用故事板。)以下是您可以使用的方法。我刚刚做了一个测试应用程序,我确认它工作得很好。代码: #im

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

以下是您可以使用的方法。我刚刚做了一个测试应用程序,我确认它工作得很好。代码:

#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

这是你可以做到的。我刚刚做了一个测试应用程序,我确认它工作得很好。代码:

#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

实际上,您应该使用
UIAppDelegate的
applicationidentinterbackground
应用程序将进入前台
委托方法,或者通过注册到相应的系统通知来完成此操作(
didBecomeActive
在其他情况下也会被调用,例如当
UIAlertView
从屏幕中退出时)

这应该是以下几行中的内容(可能包括语法问题,我在这里编写文本框):

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

    
    [[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(willEnterForeground:)名称:UIApplicationWillEnterForegroundNotification对象:nil];
    [[NSNotificationCenter defaultCenter]addObserver:自选择器:@selector(didEnterBackground:)名称:UIApplicationIdentinterBackgroundNotification对象:nil];
    

  • 实现
    willEnterForeground:
    dienterbackground:
    方法。在
    willEnterForeground:
    中,使用
    CACurrentMediaTime()或
    [NSDate-date]
    .In
    dienterbackground:
    再次采样时间并计算时间差。由于此方法是在视图控制器内实现的,因此您可以根据需要操作
    self.view
    的子视图

  • 不要忘记删除
    dealloc
    方法上的观察者(
    viewDidUnload
    自iOS 6.0以来已被弃用,因此要小心):

    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationIdentinterBackgroundNotification对象:nil]
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationWillEnterForegroundNotification对象:nil]
    


实际上,您应该使用
applicationidenterbackground
应用程序将进入
UIAppDelegate
的前台
委托方法,或者通过注册到适当的系统通知来完成此操作(
didBecomeActive
在其他情况下也会被调用,例如当
UIAlertView
从屏幕中退出时)

这应该是以下几行中的内容(可能包括语法问题,我在这里编写文本框):

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

    
    [[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(willEnterForeground:)名称:UIApplicationWillEnterForegroundNotification对象:nil];
    [[NSNotificationCenter defaultCenter]addObserver:自选择器:@selector(didEnterBackground:)名称:UIApplicationIdentinterBackgroundNotification对象:nil];
    

  • 实现
    willEnterForeground:
    dienterbackground:
    方法。在
    willEnterForeground:
    中,使用
    CACurrentMediaTime()或
    [NSDate-date]
    .In
    dienterbackground:
    再次采样时间并计算时间差。由于此方法是在视图控制器内实现的,因此您可以根据需要操作
    self.view
    的子视图

  • 不要忘记删除
    dealloc
    方法上的观察者(
    viewDidUnload
    自iOS 6.0以来已被弃用,因此要小心):

    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationIdentinterBackgroundNotification对象:nil]
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationWillEnterForegroundNotification对象:nil]
    


因为在这里设置计时器的唯一原因是在您返回前台后测量时间(而不是在后台计时任务),最好对两个事件的时间进行采样,而不是使用计时器。另外,请参阅以获取有关后台的
NSTimer
s的更多信息。非常感谢。如果我希望视图是另一个视图,该怎么办?(很抱歉显示我的新手身份。它可以是您想要的任何视图。只需确保在AppDelegate中导入视图的标题。您可以将
theViewController=[[UIViewController alloc]initWithNibName:@“theView”bundle:nil]替换为
theViewController=[[UIViewController alloc]initWithNibName:@“theView”bundle:nil]
任何你想要的东西。你能给我一个例子吗。我试过这么做,但什么也没做。FirstScreen=[[UIViewController alloc]initWithNibName:@“PLCFirstScreenView”bundle:nil];[self.FirstScreen presentViewController:FirstScreen动画:是完成:NULL];因为在这里设置计时器的唯一原因是在返回前台后测量时间(而不是在后台计时任务),最好对两个事件的时间进行采样,而不是使用计时器。另外,请参阅以获取有关后台的
NSTimer
s的更多信息。非常感谢。如果我希望视图是另一个视图,该怎么办?(很抱歉显示我的新用户。它可以是您想要的任何视图。只需确保在AppDelegate中导入视图的标题。您可以替换vi