nsnotification

How do I check if an UIViewController is currently being displayed?

烂漫一生 提交于 2019-11-30 16:40:15
问题 How do I check if an UIViewController is currently being displayed? My UIViewControllers are listening for NSNotifications - even if when they are not displayed (ie not shown). So I could have 10 UIViewController in the background observing NSNotifications from NSNotificationCenter . When an NSNotification is posted and received by the UIViewController , I'd like to find out if it is currently being shown. If it is not, I will just set a boolean so that it will processed when the View is

removeObserver with NSNotification… what am I doing wrong?

一世执手 提交于 2019-11-30 02:58:20
Basically, I have a view1 which at some point, calls view2 (via presentModalViewController:animated: ). When a certain UIButton in view2 is pressed, view2 is calls a notification method in view1 and immediately afterward is dismissed. The notification method pops up an alert. The notification method works fine and is called appropriately. The problem is, every time view1 is created (only one view1 should exist at a time), I presumably get another NSNotification being created because if I go from view0 (the menu) to view1, then back and forth a few times, I get a series of the same alert

UIImagePickerController: Detecting Camera button (shutter) pressed

情到浓时终转凉″ 提交于 2019-11-29 12:37:27
I would like to call a method that takes an NSNotification immediately after the user presses the camera shutter (i.e when the "Preview" tab bar has the buttons "Retake" and "Use"). I can't use the didFinishPickingImage method because at this time the user has already pressed the "Use" button. I have already implemented this by cameraOverlayView property of UIImagePickerController(see comments), but I wonder whether there are quicker ways of 'observing' this action. Any ideas? Axel To learn about camera button press event, you can fire a NSNotification for it. // Add observer for when camera

unexpectedly found nil while unwrapping an Optional value keyboardWillShow

♀尐吖头ヾ 提交于 2019-11-29 11:56:43
I have this code below which runs when the keyboardWillShowNotification is called: func keyboardWillShow(_ notification: Notification) { //ERROR IN THE LINE BELOW keyboard = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue animaton = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue UIView.animate(withDuration: 0.4, animations: { () -> Void in self.scrollView.frame.size.height = self.scrollViewHeight - self.keyboard.height }) } I am getting an error on the second line saying: unexpectedly found nil while unwrapping an

UIImagePickerController: Detecting Camera button (shutter) pressed

我只是一个虾纸丫 提交于 2019-11-28 06:31:21
问题 I would like to call a method that takes an NSNotification immediately after the user presses the camera shutter (i.e when the "Preview" tab bar has the buttons "Retake" and "Use"). I can't use the didFinishPickingImage method because at this time the user has already pressed the "Use" button. I have already implemented this by cameraOverlayView property of UIImagePickerController(see comments), but I wonder whether there are quicker ways of 'observing' this action. Any ideas? 回答1: To learn

How to create a class to send and receive events through NSNotificationCenter in Objective-C?

不羁的心 提交于 2019-11-27 21:41:21
I need to create two classes and both should be able to send and receive the events through the NSNotificationCenter methods.ie Both should have the sendEvent and receiveEvent methods: @implementation Class A -(void)sendEvent { addObserver:--- name:---- object:--- } -(void)ReceiveEvent { postNotificationName: --- object:--- } @end Same as such another class say ClassB should also be able to send and receive the events. How can it be done? Ideally an object would start observing interesting events as soon as its initialized. So it will register all interesting events with the NotificationCenter

How do you create custom notifications in Swift 3?

人盡茶涼 提交于 2019-11-27 17:19:10
In Objective-C, a custom notification is just a plain NSString, but it's not obvious in the WWDC version of Swift 3 just what it should be. You could also use a protocol for this protocol NotificationName { var name: Notification.Name { get } } extension RawRepresentable where RawValue == String, Self: NotificationName { var name: Notification.Name { get { return Notification.Name(self.rawValue) } } } And then define your notification names as an enum anywhere you want. For example: class MyClass { enum Notifications: String, NotificationName { case myNotification } } And use it like

How to create a class to send and receive events through NSNotificationCenter in Objective-C?

谁都会走 提交于 2019-11-26 23:05:47
问题 I need to create two classes and both should be able to send and receive the events through the NSNotificationCenter methods.ie Both should have the sendEvent and receiveEvent methods: @implementation Class A -(void)sendEvent { addObserver:--- name:---- object:--- } -(void)ReceiveEvent { postNotificationName: --- object:--- } @end Same as such another class say ClassB should also be able to send and receive the events. How can it be done? 回答1: Ideally an object would start observing

How do you create custom notifications in Swift 3?

半世苍凉 提交于 2019-11-26 18:55:42
问题 In Objective-C, a custom notification is just a plain NSString, but it's not obvious in the WWDC version of Swift 3 just what it should be. 回答1: You could also use a protocol for this protocol NotificationName { var name: Notification.Name { get } } extension RawRepresentable where RawValue == String, Self: NotificationName { var name: Notification.Name { get { return Notification.Name(self.rawValue) } } } And then define your notification names as an enum anywhere you want. For example:

What is NSNotification?

五迷三道 提交于 2019-11-26 09:43:37
问题 Can anybody explain the importance of NSNotificationCenter? Where to use them? What is the difference between NSNotificationCenter vs. AppDelegate? 回答1: Apple has provided an Observer Pattern in the Cocoa library called the NSNotificationCenter. The basic idea is that a listener registers with a broadcaster using some predefined protocol. At some later point, the broadcaster is told to notify all of its listeners, where it calls some function on each of its listeners and passes certain