首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Swift 3.1中,NotificationCenter返回batteryLevel和batteryState的正确语法是什么

在Swift 3.1中,NotificationCenter返回batteryLevel和batteryState的正确语法是什么
EN

Stack Overflow用户
提问于 2017-07-01 01:47:18
回答 1查看 706关注 0票数 1

我是Swift的新手(使用Xcode 8.3.3,Swift 3.1),我正在尝试显示电池电量和电池状态,并在值发生变化时更新它们。这是我到目前为止所知道的:

代码语言:javascript
复制
import UIKit
class ViewController: UIViewController {

@IBOutlet weak var myBatteryPercent: UILabel!
@IBOutlet weak var myBatteryState: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    UIDevice.current.isBatteryMonitoringEnabled = true


    NotificationCenter.default.addObserver(self, selector: Selector(("batteryLevelDidChange:")),
     name: NSNotification.Name.UIDeviceBatteryLevelDidChange,
     object: nil)

    NotificationCenter.default.addObserver(self, selector: Selector(("batteryStateDidChange:")),
     name: NSNotification.Name.UIDeviceBatteryStateDidChange,
     object: nil)

    var batteryLevel: Float {
        return UIDevice.current.batteryLevel
    }

    var batteryState: UIDeviceBatteryState {
        return UIDevice.current.batteryState
    }

    switch batteryState {
    case .unplugged, .unknown:
        myBatteryState.text = "not charging"
    case .charging, .full:
        myBatteryState.text = "charging or full"
    }

    myBatteryPercent.text = "\(Int((batteryLevel) * 100))%"



    func batteryLevelDidChange (notification: Notification) {
        myBatteryPercent.text = "\(Int((batteryLevel) * 100))%"
    }
    func batteryStateDidChange (notification: Notification) {
        switch batteryState {
        case .unplugged, .unknown:
            myBatteryState.text = "not charging"
        case .charging, .full:
            myBatteryState.text = "charging or full"
        }
    }    
}

当级别或状态更改时,我的应用程序崩溃,生成以下错误消息: Battery_Display.ViewController batteryLevelDidChange::unrecognized selector sent to instance 0x100b0cf10‘。

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-01 02:27:42

基本上,通知处理程序的位置是错误的。处理程序是在viewDidLoad方法中声明的,它们超出了NotificationCenter单例的作用域。只需将它们放在viewDidLoad之外,它就会起作用:

代码语言:javascript
复制
import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var myBatteryPercent: UILabel!
    @IBOutlet weak var myBatteryState: UILabel!
    var batteryLevel: Float {
        return UIDevice.current.batteryLevel
    }

    var batteryState: UIDeviceBatteryState {
        return UIDevice.current.batteryState
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        UIDevice.current.isBatteryMonitoringEnabled = true


        NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelDidChange), name: NSNotification.Name.UIDeviceBatteryLevelDidChange, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(batteryStateDidChange), name: NSNotification.Name.UIDeviceBatteryStateDidChange, object: nil)

        switch batteryState {
        case .unplugged, .unknown:
            myBatteryState.text = "not charging"
        case .charging, .full:
            myBatteryState.text = "charging or full"
        }

        myBatteryPercent.text = "\(Int((batteryLevel) * 100))%"

    }

    func batteryLevelDidChange (notification: Notification) {
        myBatteryPercent.text = "\(Int((batteryLevel) * 100))%"
    }

    func batteryStateDidChange (notification: Notification) {
        switch batteryState {
        case .unplugged, .unknown:
            myBatteryState.text = "not charging"
        case .charging, .full:
            myBatteryState.text = "charging or full"
        }
    }
}    
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44852267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档