Ios 在应用程序后切换视图IDBECOMEACTIVE
我目前正在开发一个应用程序,在后台运行超过五分钟后需要返回到另一个视图。为了做到这一点,我必须有一个定时器在按下Home(主页)按钮后在后台运行,或者在出现短信或电话中断的情况下,五分钟后,应用程序将需要转到另一个视图。我知道必须使用ApplicationIDBecomeactive方法,但如何使用?我还知道可以在ApplicationIDBecMeactive中刷新视图,但如何做到这一点?(我不使用故事板。)以下是您可以使用的方法。我刚刚做了一个测试应用程序,我确认它工作得很好。代码:Ios 在应用程序后切换视图IDBECOMEACTIVE,ios,Ios,我目前正在开发一个应用程序,在后台运行超过五分钟后需要返回到另一个视图。为了做到这一点,我必须有一个定时器在按下Home(主页)按钮后在后台运行,或者在出现短信或电话中断的情况下,五分钟后,应用程序将需要转到另一个视图。我知道必须使用ApplicationIDBecomeactive方法,但如何使用?我还知道可以在ApplicationIDBecMeactive中刷新视图,但如何做到这一点?(我不使用故事板。)以下是您可以使用的方法。我刚刚做了一个测试应用程序,我确认它工作得很好。代码: #im
#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]
.Indienterbackground:
再次采样时间并计算时间差。由于此方法是在视图控制器内实现的,因此您可以根据需要操作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]
.Indienterbackground:
再次采样时间并计算时间差。由于此方法是在视图控制器内实现的,因此您可以根据需要操作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