Tag: nsnotificationcenter

什么是通知(Swift3)?

最近,有一位朋友告诉我,在最近的一次采访中,有人问她有关Notification的经历。 我认为稍微研究一下会很有用。 我了解到的一件事是,通知中心(以前称为NSNotificationCenter)适合观察者设计模式,该对象在其中将任何更新或更改通知其他对象。 Apple的文档指出,“ NSNotificationCenter对象(或简称为通知中心)提供了一种在程序内广播信息的机制。”换句话说,Notification就像广播消息的广播电台一样。 这样, 发送者 (广播者或广播电台)发出消息, 观察者 (听者)可以决定何时收听以及收听什么内容。 基本上,通知提供了一种在应用程序内传递信息的方法,而类之间没有直接引用。 通知中心会跟踪关注特定通知的所有不同类型的观察者,因此在发布通知时,通知中心会将消息发布到已为该通知注册的每个观察者。 要设置通知中心,需要3个步骤: 步骤1:将事件(消息)发布到通知中心,该事件将通知要侦听此特定消息的所有对象。 步骤2:将观察者(侦听器)添加到通知中心。 步骤3:编写一个代码块,该代码块在发生通知时被调用(收到通知后您将要做什么?)。 我制作了一个带有标签栏控制器的简单应用程序。 标签1:显示随机的海洋图像。 选项卡2:如果用户喜欢选项卡1中的图像,则选项卡2将向他们显示另一个随机海洋图像。 在上面,我向NotificationCenter.default发布了一个名为“ LikedItNotification”的通知。一旦用户单击FirstViewController中的 Like Like按钮,该通知就会被发布! 在上面,我为在FirstViewController中创建的通知创建了一个观察者(侦听器)。 我还编写了将在收到通知后执行操作的代码。 在这种情况下,我的操作是取消隐藏youLikedItLabel并将随机图像分配给SecondViewController中的imageView。 埃里卡(Erica),如果您在想,我觉得您可以通过使用单例来做类似的事情,您是对的。 看来NotificationCenter只是让您的班级互相交谈的另一种方式。 我的理解是,如果整个应用程序需要了解特定的通知(即显示键盘,用户确实登录/注销),则可以最佳地利用NotificationCenter。 这是我完成的应用程序的演示视频! 希望您今天了解到有关NotificationCenter的新知识。 您可以在此处下载项目。 资源: Apple文档— NSNotificationCenter 视频— Swift 3中的iOS通知中心 停止使用NSNotificationCenter Ray Wenderlich — iOS设计模式:观察者模式

发布后在connectionDidFinishLoading:,NSURLConnection委托方法内不能收到通知

我无法收到通知: 一个类NSURLConnectionasynchronous的东西 – (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:_receivedData, @"receivedData", nil]; [[NSNotificationCenter defaultCenter] postNotificationName:kWebServiceURLTemperaturaMaximaKMLNotitificationName object:self userInfo:userInfo]; } 另一个class级观察通知: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceivedCompleteData:) name:kWebServiceURLTemperaturaMaximaKMLNotitificationName object:self]; 有什么想法吗?

在不加载视图的情况下接收通知

正如我在这个问题中解释的,我试图将数据从一个TableView中的UISwitch传递到另一个ViewController。 但是,我希望在设置视图(包含UISwitch)被解散时传递数据。 在这个伟大的答案的第三个步骤之后,我使用UserDefaults来传递数据,并尝试使用NotificationCenter来更新UserDefaults值,当解散设置视图。 要更新用户默认值更改时,您可以让您的设置控制器解雇时发布通知。 然后,您应用中的其他VC可以侦听此通知并根据需要进行更新。 我已经尝试检索viewDidLoad内的通知,但值不会刷新,因为解散设置视图不会每次加载新的ViewController。 每次设置视图被解除时,我怎样才能接收/阅读通知? 更确切地说,我在哪里以及如何编写阅读通知的代码? 以下是发布通知的代码: class SettingsViewController: UIViewController { @IBAction func doneButton(_ sender: UIButton) { self.dismiss(animated: true, completion: nil) NotificationCenter.default.post(name: Notification.Name("nSound"), object: nil) } 以下是检索通知的代码: class ViewController: UIViewController { @IBOutlet weak var testLabel: UILabel! var savedValue = UserDefaults.standard.bool(forKey: "sound") override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(ViewController.viewDidLoad), name: Notification.Name("nSound"), object: nil) […]

多重select在IOS中不能正常工作

我有一个问题,在iOS表格视图中设置行的复选标记如果我select上面的一个元素,下一个第13个元素也被选中,我不知道为什么? 在设置复选标记之前,我需要对表格做些什么,因为我只是检查一个条件,如果这种情况是真的,我将把accessoryType设置为复选标记,下面是代码。 注意: – 发生这种情况时,第13行不会被选中,只会改变该行的配件types。 if let cell = tableView.cellForRowAtIndexPath(indexPath) { if cell.selected { if(self.sections[indexPath.section].files[indexPath.row].type != "cloud"){ print(self.sections[indexPath.section].files[indexPath.row]) cell.accessoryType = .Checkmark NSNotificationCenter.defaultCenter().postNotificationName("enableOptions", object: nil) } } } CellForIndexPath代码: override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MyFilesTableViewCell let fileSection = sections[indexPath.section] let file = fileSection.files[indexPath.row] cell.title.text […]

为什么这个audio会话无法识别中断?

我的应用程序从查找表合成audio。 它能够成功播放audio,但是当我尝试停止播放的时候会崩溃。 audio播放只需要在不重新启动的情况下退出,因此处理中断的要求是基本的。 我重读了Apple的audio会话编程指南,包括“ 响应中断 ”一节。 然而,方法handleAudioSessionInterruption似乎并没有注册一个中断,所以我显然缺less一些东西。 编辑看到我的答案。 当我开始工作时,我几乎不知道NSNotificationCenter所以我欢迎任何改进build议。 两种方法设置audio会话在前台播放。 – (void)setUpAudio { if (_playQueue == NULL) { if ([self setUpAudioSession] == TRUE) { [self setUpPlayQueue]; [self setUpPlayQueueBuffers]; } } } – (BOOL)setUpAudioSession { BOOL success = NO; NSError *audioSessionError = nil; AVAudioSession *session = [AVAudioSession sharedInstance]; // Set up notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAudioSessionInterruption:) […]

使用NotificationCenter Observer处理asynchronous请求

这个问题也有类似的问题,所以我很抱歉,但是没有人能够帮助我。 我正努力将这个asynchronous请求的值从完成处理程序返回给Firebase。 我从Firebase中检索的值是一个数组,它确实存在。 但 以下是我向Firebase发出请求的function: class SearchManager { var searchResults = [String]() var listOfMosaics = [String]() // Retrieves company list from Firebase func getMosaicTitles(completionHandler: @escaping (_ mosaics: [String]) -> ()) { Database.database().reference().child("mosaics").observeSingleEvent(of: .value, with: { (snapshot) in guard let allMosaics = snapshot.value as? [String] else { print("unable to unwrapp datasnapshot") return } completionHandler(allMosaics) }) } […]

设备通知或值更改,find它在其他设备

我正在开发一些应用程序,应该通知我当前的设备值在某些区域内的其他设备。 比方说,我有应用程序,同样的应用程序安装在其他3个设备。 当我改变我的设备中的东西时,应该通知其他设备。 就像我select一个button,那个buttonselect也应该在其他3个设备上。 是否有可能没有networking服务,有没有在iOS的任何特定的框架来完成这项任务。 这种区域内的蓝牙search设备。 它应该在房间内,没有任何互联网或无线networking的东西。 任何人都可以帮助我find解决scheme。 谢谢,

应用程序终止后,使用NSNotification在UIViewController中处理UILocalNotification

我正在使用UILocalNotification ,我想通知我的控制器之一,即使应用程序已被终止,通知已收到。 在我的appDelegate中,我实现了这个function: -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { if ([application applicationState] == UIApplicationStateInactive) { [[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:notification.userInfo]; } } 在我的UIViewController我在viewDidLoad方法上实现了观察者 – (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didlocalNotificationReceived:) name:@"localNotificationReceived" object:nil]; } 它运行在后台运行的应用程序完美。 由于viewDidLoad方法已经被调用,Observer正在等待.. 问题是当我杀了应用程序。 然后,我的控制器的观察者不见了, didlocalNotificationReceived方法从不被调用。 我认为这是因为当我收到localNotification并再次运行应用程序。 didReceiveLocalNotification:方法在我的UIViewController的viewDidLoad之前调用。 然后在PostNotificationName之后创build观察者,然后观察者不收到任何东西。 我想知道是否有一些最佳实践或模式来处理这类问题。 我知道didFinishLaunchingWithOptions方法是在didlocalNotificationReceived之前didlocalNotificationReceived因此可能有些事情要做。 更新: 我也发现,当应用程序被终止。 一旦你点击通知,它打开应用程序,调用函数didFinishLaunchingWithOptions但从来没有调用didReceiveLocalNotification 。 所以我认为我会处理两种情况。

NSNotificationCenter Swift 3.0在键盘上显示和隐藏

我想在键盘显示和消失时运行一个函数,并具有以下代码: let notificationCenter = NotificationCenter.default notificationCenter.addObserver(self, selector: #selector(ViewController.keyBoardUp(Notification :)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 而下面的functionkeyBoardUp : func keyBoardUp( Notification: NSNotification){ print("HELLO") } 但是,键盘显示时,该function不会打印到控制台。 帮助将不胜感激

睡眠模式与通知中心打开

在两个,1)iPhone进入睡眠模式2)打开通知中心 它叫 – (void)applicationWillResignActive:(UIApplication *)application 有没有什么方法可以确定是因为设备进入睡眠模式还是通过打开通知中心来调用此方法?