如何在Swift中将观察者添加到默认通知中心?我正在尝试移植这行代码,以便在电池电量发生变化时发送通知.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
Run Code Online (Sandbox Code Playgroud) 我正在socket.io我的swift ios应用程序中实现.
目前在几个面板上我正在监听服务器并等待收到的消息.我这样做是通过调用getChatMessage每个面板中的函数来实现的:
func getChatMessage(){
SocketIOManager.sharedInstance.getChatMessage { (messageInfo) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
//do sth depending on which panel user is
})
}
}
Run Code Online (Sandbox Code Playgroud)
但是我注意到这是一个错误的方法,我需要更改它 - 现在我想开始只收听一次传入消息,当任何消息到来时 - 将此消息传递给任何监听它的面板.
所以我想通过NSNotificationCenter传递传入的消息.到目前为止,我能够传递发生的事情的信息,但不能传递数据本身.我是这样做的:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.showSpinningWheel(_:)), name: showSpinner, object: nil)
Run Code Online (Sandbox Code Playgroud)
然后我有一个叫做的函数:
func showSpinningWheel(notification: NSNotification) {
}
Run Code Online (Sandbox Code Playgroud)
任何时候我想打电话给我:
NSNotificationCenter.defaultCenter().postNotificationName(hideSpinner, object: self)
Run Code Online (Sandbox Code Playgroud)
那么如何传递对象messageInfo并将其包含在被调用的函数中?
我有一个buttonA,当单击buttonA时,我希望键盘在inputAccessoryView中显示UITextField.有没有办法让键盘显示手动,并设置inputAccessoryView最初没有UITextField?
谢谢.
这是我在swift 2中的代码.
如何在swift 3中使用相同的东西?
NotificationCenter.default.addObserver(self, selector: "handleInterruption", name: AVAudiosessionInterruptionNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我知道 NotificationCenter 已更改,并且我已查找如何使用此链接将其更改为新的实现: Swift 3 上的 NotificationCenter 问题,但我仍然无法让我的工作!我正在使用课堂教科书做我班上的作业,这是我目前的班级:
//
// ViewController.swift
// Persistence
//
// Created by Skyleguy on 10/31/16.
// Copyright © 2016 Skyleguy. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var lineFields: [UITextField]!
override func viewDidLoad() {
super.viewDidLoad()
let filePath = self.dataFilePath()
if (FileManager.default.fileExists(atPath: filePath))
{
let array = NSArray(contentsOfFile: filePath) as! [String]
for i in 0 ..< array.count
{
lineFields[i].text = array[i]
}
}
let notificationName = Notification.Name("applicationWillResignActive")
NotificationCenter.default.addObserver(self, selector: #selector(Persistence.applicationWillResignActive(notification: NSNotification)), …Run Code Online (Sandbox Code Playgroud)