无法识别的选择器由NSNotificationCenter发送到实例

时间:2015-07-27 16:31:49

标签: ios objective-c swift

当我想通过NSNotificationCenter更新标签时会出现运行时错误,但我不知道原因。选民的名字相同。

ViewController2

@IBAction func saveSettings(sender: UIButton) {
    NSNotificationCenter.defaultCenter().postNotificationName("TimeRepairID", object: nil)
}

ViewController

    override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "setTimeRepair", name: "TimeRepairID", object: nil)  
}

func setTimeRepair(notification: NSNotification){
    CurrentTimerepair.text = String(format:"%.1f", RepairTime.sharedInstance.TimeOfRepair())
    RFPTime.text = String(RepairTime.sharedInstance.ReadyForPickup())
}

所以每当我按下ViewController2中的保存按钮时,应用程序崩溃,发送到实例0x13e63bd40的无法识别的选择器知道任何

1 个答案:

答案 0 :(得分:1)

当你将NSNotification作为参数传递时,你在选择器名称之后错过了:

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "setTimeRepair:", name: "TimeRepairID", object: nil)
}

func setTimeRepair(notification: NSNotification){
    CurrentTimerepair.text = String(format:"%.1f", RepairTime.sharedInstance.TimeOfRepair())
    RFPTime.text = String(RepairTime.sharedInstance.ReadyForPickup())
}