从另一个视图更新文本标签 [英] Update Text Label From Another View
问题描述
我有一个 NSTimer
,它每 10 秒运行一次,并从 LandingController.m 启动.当您转到应用程序中的其他视图时,它会继续运行.我希望能够(当在该计时器内满足特定条件时)从另一个视图 GuardMenu.m 更新标签字段我要更新的标签称为 CurrentZone.text,我想将其从值N"更新为值Y".
I have an NSTimer
that runs every 10 seconds and is kicked off from LandingController.m. It continues to run as you go to other views in the application. I want to be able to (when a certain condition is met within that timer) update a label field from another view GuardMenu.m The label I want to update is called CurrentZone.text and I want to update it from value "N" to value "Y."
这是我在 LandingController.m 上的计时器
Here's my timer on LandingController.m
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(checkForMessages)
userInfo:nil
repeats:YES];
在 LandingController.m 上调用它
Which calls this on 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"
}
}
推荐答案
首先在你的GuardMenu类的init
方法中创建一个NSNotification
First create a NSNotification in your init
method of GuardMenu class
- (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;
}
然后实现通知的选择器,您将在此处更改CurrentZone
标签文本.
Then implement the notification's selector, this is where you will be changing your CurrentZone
label text.
- (void)receiveNotification:(NSNotification *) notification {
if ([[notification name] isEqualToString:@"TextChangeNotification"]) {
NSLog (@"Change you label here");
self.lblCurrentZone.text = @"Y";
}
}
现在在你的 LandingViewController.m
-viewDidLoad
方法
启动计时器.
self->msgTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(checkForMessages) userInfo:nil repeats:YES];
现在为 NSTimer 实现 @selector
,这是你将通知发送回 GuardMenu 类的地方
Now implement the @selector
for the NSTimer, 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 {
}
}
注意:NotificationName
应该相同.
这篇关于从另一个视图更新文本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!