NCo*_*der -1 objective-c nstimer ios
我有一个NSTimer每10秒运行一次,并从LandingController.m开始.当您转到应用程序中的其他视图时,它将继续运行.我希望能够(当在该计时器内满足某个条件时)从另一个视图更新标签字段GuardMenu.m我想要更新的标签名为CurrentZone.text,我想将它从值"N"更新为值"Y".
这是我在LandingController.m上的计时器
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(checkForMessages)
userInfo:nil
repeats:YES];
Run Code Online (Sandbox Code Playgroud)
在LandingController.m上调用它
- (void)checkForMessages
{
if ( //some condition here ){
//update CurrentZone.text label in GuardMenu view and set equal to "Y"
} else {
//no need to update label in GuardMenu as it's currently equal to "N"
}
}
Run Code Online (Sandbox Code Playgroud)
首先在initGuardMenu类的方法中创建一个NSNotification
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"TextChangeNotification" object:nil];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
然后实现通知的选择器,这是您将更改CurrentZone标签文本的位置.
- (void)receiveNotification:(NSNotification *) notification {
if ([[notification name] isEqualToString:@"TextChangeNotification"]) {
NSLog (@"Change you label here");
self.lblCurrentZone.text = @"Y";
}
}
Run Code Online (Sandbox Code Playgroud)
现在在你的LandingViewController.m -viewDidLoad方法中
启动计时器.
self->msgTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(checkForMessages) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
现在实现@selectorNSTimer,this is where you will be sending the notification back to the GuardMenu class
- (void)checkForMessages {
NSString *strText = @"test";
if ([strText isEqualToString:@"test"]){
[[NSNotificationCenter defaultCenter] postNotificationName:@"TextChangeNotification" object:nil];
}
else {
}
}
Run Code Online (Sandbox Code Playgroud)
注:在NotificationName应该是相同的.
| 归档时间: |
|
| 查看次数: |
1309 次 |
| 最近记录: |