Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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."

Here's my timer on LandingController.m

self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
                        target:self
                        selector:@selector(checkForMessages)                                                                     
                        userInfo:nil
                        repeats:YES];

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"

        }


    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
202 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;
}

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";
    }
}

Now in your LandingViewController.m -viewDidLoad Method

Start the timer.

self->msgTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(checkForMessages) userInfo:nil repeats:YES]; 

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 {

    }
}

NOTE: the NotificationName should be the same.

Sample Project Code Dropbox Link


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...