Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通知中心不工作。观察员没有被叫来_Ios_Swift_Nsnotificationcenter - Fatal编程技术网

Ios 通知中心不工作。观察员没有被叫来

Ios 通知中心不工作。观察员没有被叫来,ios,swift,nsnotificationcenter,Ios,Swift,Nsnotificationcenter,我试图从Swift中的另一个类调用函数,NotificationCenter是一个选项,因此我从addObserver开始 override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(toggleSideMenu), name: NSNotification.Name("callToggleSideMenu")

我试图从Swift中的另一个类调用函数,
NotificationCenter
是一个选项,因此我从
addObserver
开始

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(toggleSideMenu), name: NSNotification.Name("callToggleSideMenu"), object: nil)
}

@objc func toggleSideMenu(){
    if isMenuOpen {
        sideContainer.constant = -260
    } else {
        sideContainer.constant = 0
    }
}
在另一节课上,我增加了(帖子):


一切似乎都好,但我不知道为什么它不工作。我在stackoverflow中看到了很多相同的问题,但没有答案解决了我的问题

函数定义不正确。应该是:

@objc func toggleSideMenu(_ notification: Notification){
    if isMenuOpen {
        sideContainer.constant = -260
    } else {
        sideContainer.constant = 0
    }
 }  
使用以下命令调用它:

NotificationCenter.default.addObserver(self, selector: #selector(toggleSideMenu(_:)), name: NSNotification.Name("callToggleSideMenu"), object: nil)

函数定义不正确。应该是:

@objc func toggleSideMenu(_ notification: Notification){
    if isMenuOpen {
        sideContainer.constant = -260
    } else {
        sideContainer.constant = 0
    }
 }  
使用以下命令调用它:

NotificationCenter.default.addObserver(self, selector: #selector(toggleSideMenu(_:)), name: NSNotification.Name("callToggleSideMenu"), object: nil)

addObserver
是在post之前调用的吗?
addObserver
是在post之前调用的吗?选择器应该是
#selector(toggleSideMenu)
,或者
#selector(toggleSideMenu)
,而不是
选择器(toggleSideMenu:)
。选择器应该是
#selector(toggleSideMenu)
,或者
#selector(toggleSideMenu(:)
,而不是
#选择器(toggleSideMenu:)