jjzjj

addobserver

全部标签

objective-c - 在 UIViewController 中向 NSNotificationCenter 添加和删除观察者

查看各种Apple示例(例如AddMusic),我看到他们将观察者添加到viewDidLoad中的默认NSNotificationCenter,然后在中删除它们释放。这看起来很危险,因为可以多次调用viewDidLoad而无需调用dealloc。这会多次添加同一个观察者,导致处理程序被多次调用。一个解决方案是在viewDidUnload中也删除观察者,但这意味着同一个观察者可以在dealloc中第二次被删除,这看起来像一个潜在的问题。我错过了什么? 最佳答案 有很多关于以正确方式删除通知的讨论。例如:removeobserver-w

objective-c - 在 UIViewController 中向 NSNotificationCenter 添加和删除观察者

查看各种Apple示例(例如AddMusic),我看到他们将观察者添加到viewDidLoad中的默认NSNotificationCenter,然后在中删除它们释放。这看起来很危险,因为可以多次调用viewDidLoad而无需调用dealloc。这会多次添加同一个观察者,导致处理程序被多次调用。一个解决方案是在viewDidUnload中也删除观察者,但这意味着同一个观察者可以在dealloc中第二次被删除,这看起来像一个潜在的问题。我错过了什么? 最佳答案 有很多关于以正确方式删除通知的讨论。例如:removeobserver-w

Swift 4 - 通知中心 addObserver 问题

每次Notification到达并且App尝试执行其关联方法时,我都会崩溃并收到unrecognizedselector错误。这是我的代码-在viewDidLoad中:letnotificationCenter=NotificationCenter.defaultnotificationCenter.addObserver(self,selector:Selector(("sayHello")),name:NSNotification.Name(rawValue:"dataDownloadCompleted"),object:nil)sayHello()方法非常简单-如下所示:func

Swift 4 - 通知中心 addObserver 问题

每次Notification到达并且App尝试执行其关联方法时,我都会崩溃并收到unrecognizedselector错误。这是我的代码-在viewDidLoad中:letnotificationCenter=NotificationCenter.defaultnotificationCenter.addObserver(self,selector:Selector(("sayHello")),name:NSNotification.Name(rawValue:"dataDownloadCompleted"),object:nil)sayHello()方法非常简单-如下所示:func

ios - 如何在 Swift 3 中编写键盘通知

我正在尝试将此代码更新为swift3:NSNotificationCenter.defaultCenter().addObserver(self,selector:Selector("keyboardWillShow:"),name:UIKeyboardWillShowNotification,object:nil)NSNotificationCenter.defaultCenter().addObserver(self,selector:Selector("keyboardWillHide:"),name:UIKeyboardWillHideNotification,object:n

ios - 如何在 Swift 3 中编写键盘通知

我正在尝试将此代码更新为swift3:NSNotificationCenter.defaultCenter().addObserver(self,selector:Selector("keyboardWillShow:"),name:UIKeyboardWillShowNotification,object:nil)NSNotificationCenter.defaultCenter().addObserver(self,selector:Selector("keyboardWillHide:"),name:UIKeyboardWillHideNotification,object:n

ios - 无法在 NSNotificaitonCenter 的 addObserver : selector: name: object: method 中使用 UIApplicationDidEnterBackgroundNotification 作为名称

我将一个文件复制到一个新项目中,它似乎工作正常。这段代码也在旧项目中工作,但突然之间(在新项目中),我遇到了几个我无法弄清楚的错误。NSNotificationCenter*center=[NSNotificationCenterdefaultCenter];[centeraddObserver:selfselector:@selector(saveBookmarks)name:UIApplicationWillTerminateNotificationobject:nil];[centeraddObserver:selfselector:@selector(saveBookmarks

ios - 在第一个 View Controller 中添加 'addObserver' (NSNotificationCenter),在第二个 View Controller 中处理

这个问题在这里已经有了答案:SendandreceivemessagesthroughNSNotificationCenterinObjective-C?(6个答案)关闭9年前。我看到了一些关于在同一个类中添加观察者和句柄的例子,但我想知道是否可以在第一个ViewController中添加观察者并在第二个ViewController中处理它?我想不断地从第一个ViewController发送距离并在第二个ViewController中处理它。作为subview添加的第二个ViewController:addSubview、addChildViewController。有点像androi

ios - 当 AVPlayer 准备好播放时,addObserver 到 AVPlayer 永远不会被调用?

我已将观察者添加到AVPlayer,但它不会在avplayer状态准备好播放时调用该函数。该函数在视频开始播放时调用两次,但AVPlayer的持续时间参数都返回“nan”,我需要在AVPlayer准备好播放时获取持续时间。playerController.player=playerself.addChildViewController(playerController)self.view.addSubview(playerController.view)playerController.view.frame=self.view.frameplayerController.showsPl

ios - NSNotificationCenter addObserver 在 Swift 中调用私有(private)方法

我使用addObserverAPI来接收通知:NSNotificationCenter.defaultCenter().addObserver(self,selector:"methodOFReceivedNotication:",name:"NotificationIdentifier",object:nil)我的方法是:funcmethodOFReceivedNotication(notification:NSNotification){//ActiontakeonNotification}是的,它有效!但是当我将方法methodOFReceivedNotication更改为pri