objective c 中会话不活动的 nsnotification 方法
nsnotification approach for session inactivity in objective c
我的项目在会话不活动实施中。我在项目的 RootViewController class 中创建了一个 NSNotification。
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @"Close"
style: UIBarButtonItemStyleDone
target: self
action: @selector(closeModal)];
UIImage *image = [UIImage imageNamed:@"fidelity_logotype"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
[imageView setImage:image];
[self.navigationItem setTitleView:imageView];
self.navigationController.view.backgroundColor = [UIColor fidelityGreen];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidTimeout:) name:@"ApplicationTimeout" object:nil];
}
- (void) applicationDidTimeout:(NSNotification *) notif
{
NSLog(@"I m here");
BCDSessionInactivityViewController *sessionView=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"InactivityViewController"];
sessionView.modalPresentationStyle = UIModalPresentationFormSheet;
sessionView.preferredContentSize = CGSizeMake(838,340);
[[self topViewController] presentViewController:sessionView animated:YES completion:nil];
}
并且在 logoutviewcontroller 中,我正在删除下面写的这个观察者
- (IBAction)logoutbtn:(id)sender
{
NSLog(@"logout is called");
[sessionTimer invalidate];
sessionTimer = nil;
[[BCDTimeManager sharedTimerInstance]stopIdleTimer];
//[self dismissViewControllerAnimated:YES completion:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ApplicationTimeout" object:nil];
[self performSegueWithIdentifier:@"Thankyoupage" sender:self];
}
这是我 post 发送通知的代码。
- (void)idleTimerExceeded {
NSLog(@"idle time exceeded");
[[NSNotificationCenter defaultCenter]
postNotificationName:@"ApplicationTimeout" object:nil];
}
对于第一次登录,只要计时器超时,它就可以正常工作,我 post 完美呈现通知和模型视图,但是一旦用户注销,之后每当通知 posted ,选择器方法被调用两次
我很确定通知只 posted 一次。
我应该在每个视图控制器中创建通知,然后在视图卸载时删除它吗?
我做错了什么?
您正在 RootViewController
中添加通知并试图将其从 LogoutViewController
中删除。所以添加到 RootViewController 的通知观察者永远不会被删除。因此,每次注销和登录时,观察者调用都会增加 1。要解决此问题,您需要从 RootViewController 对象中删除观察者。
为了解决您在评论中提到的问题,
If I remove the observer in RootViewController , then if timers
exceeds in some other views, and notification observer is not called.
Also, i can't add observer on app delegate because we want timer
notification to be fired only after reaching rootviewController
- 在
AppDelegate
中写两个public方法
- 一个用于添加观察者(addObserver)
- 一个用于移除观察者 (removeObserver)
- 当到达
RootViewController
时,调用addObserver
添加观察者的方法
- 当按下注销时,调用
removeObserver
移除观察者
我的项目在会话不活动实施中。我在项目的 RootViewController class 中创建了一个 NSNotification。
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @"Close"
style: UIBarButtonItemStyleDone
target: self
action: @selector(closeModal)];
UIImage *image = [UIImage imageNamed:@"fidelity_logotype"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
[imageView setImage:image];
[self.navigationItem setTitleView:imageView];
self.navigationController.view.backgroundColor = [UIColor fidelityGreen];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidTimeout:) name:@"ApplicationTimeout" object:nil];
}
- (void) applicationDidTimeout:(NSNotification *) notif
{
NSLog(@"I m here");
BCDSessionInactivityViewController *sessionView=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"InactivityViewController"];
sessionView.modalPresentationStyle = UIModalPresentationFormSheet;
sessionView.preferredContentSize = CGSizeMake(838,340);
[[self topViewController] presentViewController:sessionView animated:YES completion:nil];
}
并且在 logoutviewcontroller 中,我正在删除下面写的这个观察者
- (IBAction)logoutbtn:(id)sender
{
NSLog(@"logout is called");
[sessionTimer invalidate];
sessionTimer = nil;
[[BCDTimeManager sharedTimerInstance]stopIdleTimer];
//[self dismissViewControllerAnimated:YES completion:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ApplicationTimeout" object:nil];
[self performSegueWithIdentifier:@"Thankyoupage" sender:self];
}
这是我 post 发送通知的代码。
- (void)idleTimerExceeded {
NSLog(@"idle time exceeded");
[[NSNotificationCenter defaultCenter]
postNotificationName:@"ApplicationTimeout" object:nil];
}
对于第一次登录,只要计时器超时,它就可以正常工作,我 post 完美呈现通知和模型视图,但是一旦用户注销,之后每当通知 posted ,选择器方法被调用两次
我很确定通知只 posted 一次。
我应该在每个视图控制器中创建通知,然后在视图卸载时删除它吗?
我做错了什么?
您正在 RootViewController
中添加通知并试图将其从 LogoutViewController
中删除。所以添加到 RootViewController 的通知观察者永远不会被删除。因此,每次注销和登录时,观察者调用都会增加 1。要解决此问题,您需要从 RootViewController 对象中删除观察者。
为了解决您在评论中提到的问题,
If I remove the observer in RootViewController , then if timers exceeds in some other views, and notification observer is not called. Also, i can't add observer on app delegate because we want timer notification to be fired only after reaching rootviewController
- 在
AppDelegate
中写两个public方法- 一个用于添加观察者(addObserver)
- 一个用于移除观察者 (removeObserver)
- 当到达
RootViewController
时,调用addObserver
添加观察者的方法 - 当按下注销时,调用
removeObserver
移除观察者