- (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 UILabel在斷電時不會改變
我做錯了什麼?
我打算只支持的iOS 5.x和6
真的從來沒有?你做了一個長期測試(幾個小時)? – Felix 2012-07-05 17:06:12
我的電池電量爲70%,假設每5%或1分鐘觸發一次。但即時通訊接近100%和UILabel從未改變 – 2012-07-05 17:15:22
我得到了通知中心工作,但UILabel沒有更新。 – 2012-07-05 20:56:11