Update Text Label From Another View

2019-09-19 16:28发布

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"

        }


    }

7条回答
淡お忘
2楼-- · 2019-09-19 17:29

Create a notification in the init of GuardMenu class

[[NSNotificationCenter defaultCenter] addObserver:self 
                                  selector:@selector(receiveNotification:) 
                                  name:@"UpdateCurrentZoneNotification" 
                                  object:nil];

In the LandingController,

  • (void)checkForMessages {

    if ( //some condition here ){
    [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateCurrentZoneNotification" 
                                  object:nil];
    //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"
    
    }
    

    }

查看更多
登录 后发表回答