【发布时间】:2012-07-05 16:57:32
【问题描述】:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device];
}
- (void)batteryChanged:(NSNotification *)notification
{
UIDevice *device = [UIDevice currentDevice];
NSLog(@"State: %i Charge: %f", device.batteryState, device.batteryLevel);
batteryLabel.text = [NSString stringWithFormat:@"State: %i Charge: %f", device.batteryState, device.batteryLevel];
}
UILabel 永远不会更新。永远不会触发电源事件。
我做错了吗?
我计划只支持 iOS 5.x 和 6
【问题讨论】:
-
真的没有吗?您是否进行了长期测试(几个小时)?
-
我的电池电量为 70%,假设每 5% 或 1 分钟触发一次。但我接近 100% 并且 UILabel 从未改变
-
通知中心工作正常,但 UILabel 没有更新。
-
可能通知不在主线程上
标签: ios5 notifications uilabel nsnotificationcenter uidevice