简体   繁体   中英

Updating UI from IOS library

Could somebody give me guidance/starting point on how I would update a UI Text Label in an IOS app from a library .

I have temperature data being received from a BT module in a library connected to my app. I want to send that Integer data to my app and update the UI Text label.

NOTE: I have full access to the library.

Any help is appreciated

I think what you are asking for is a way to provide feedback to an application from your Cocoa static library.

I would suggest that you have a look at the NSNotificationCenter class. For example, say that you have a class BTThermometer that, upon the reception of a new measurement calls:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"com.my.BTThermometer.NewValue" object:self];

Then, in you application, you could do something like:

[[NSNotificationCenter defaultCenter] addObserverForName:@"com.my.BTThermometer.NewValue" object:self queue:nil usingBlock:^(NSNotification* n) {
    dispatch_async(dispatch_get_main_queue(), ^{
        someLabel.text = ((BTThermometer*)n.object).temperatureValue;
    }
}];

This is a standard mechanism in iOS to decouple your application from the internal workings of your library. The only coupling is the name itself, and of course it is usually a good idea to use a constant (eg #define kMyTemperatureEvent @"com.my.BTThermometer.NewValue ) so that the compiler catches any typos.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM