2012-07-05 89 views
0
- (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

+0

真的从来没有?你做了一个长期测试(几个小时)? – Felix 2012-07-05 17:06:12

+0

我的电池电量为70%,假设每5%或1分钟触发一次。但即时通讯接近100%和UILabel从未改变 – 2012-07-05 17:15:22

+0

我得到了通知中心工作,但UILabel没有更新。 – 2012-07-05 20:56:11

回答

0
IBOutlet UILabel *currentBatteryStatusLabel; 

惊醒的UILabel正确连接到故事板

  • 更新的UILabel我改变上面的代码放到下面,一切开始工作。

    __weak IBOutlet UILabel *currentBatteryStatusLabel; 
    

    我不知道为什么它使这么大的差别

  • 0

    如果你得到的通知中心工作,但的UILabel没有更新,是不是仅仅是因为应用程序在后台时通报收到?

    我会尝试以下方法:

    1. 看看,一旦应用程序从睡眠状态
    +0

    该应用程序不在后台。我有屏幕设置为永不睡眠(通过设置),我一直在积极观察它。我最终通过修改出口在前面有__weak来实现它 – 2012-07-06 12:47:20