2012-07-05 21 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