ios - 如何将参数传递给 NSNotificationCenter 中的选择器?

标签 ios swift selector nsnotificationcenter

此代码检查应用程序何时激活并运行特定方法 dataMain()。我向 dataMain(productCode: String) 添加了一个参数。

NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: "dataMain",
            name: UIApplicationDidBecomeActiveNotification,
            object: nil)

有什么方法可以将 productCode 参数传递给选择器?

最佳答案

如果您在注册此通知时有 productCode 变量,那么您可以使用不同的通知观察方法。

let productCode = "A string"

NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationDidBecomeActiveNotification,
    object: nil,
    queue: NSOperationQueue.mainQueue()) { (notification) -> Void in
        self.dataMain(productCode)
}

关于ios - 如何将参数传递给 NSNotificationCenter 中的选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29976441/

相关文章:

javascript - iOS 应用程序扩展 javascript 参数在 CompleteRequest 后未定义

ios - iPhone 设备中的 Json 解析错误,但在模拟器中正确

Objective-c执行选择器: vs performSelector:withObject

javascript - 选择不在 anchor 中的跨度

objective-c - 使用未解析的标识符将 Objective-C 转换为 Swift 3.1 错误

ios - Swift iOS : Parsing of date from a string does not work for some devices. 太奇怪了

objective-c - AUSampler 出厂预设 MacError -1073748960

ios - 如何返回 numberOfRowsInSection?

swift - 创建自定义标签栏

ios - 具有最大长度的 UITextField 自动完成