iOS - 显示电池状态的问题 [英] iOS - Issue with displaying battery status

查看:142
本文介绍了iOS - 显示电池状态的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用UILabel显示电池百分比,但结果是胡说八道!这是我的代码:

I am trying to display battery percentage with UILabel , but the result is nonsense ! here is my code :

UIDevice *myDevice = [UIDevice currentDevice];
    [myDevice setBatteryMonitoringEnabled:YES];
    int i=[myDevice batteryState];
    _battery.text = [NSString stringWithFormat:@"%i",i];

标签显示数字2 !!!!

the labels shows number 2 !!!!

推荐答案

使用以下代码获取电池电量

Use below code to get battery level

UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batteryLevel = [myDevice batteryLevel];
_battery.text = [NSString stringWithFormat:@"%f",batteryLevel*100];

[myDevice batteryLevel]; 会给你电池介于0.0(空)和1.0(100%充电)之间

[myDevice batteryLevel]; will give you the battery between 0.0 (empty) and 1.0 (100% charged)

希望它有所帮助..

这篇关于iOS - 显示电池状态的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆