无法找到 NSNotification 的发件人

Unable to find sender of NSNotification

我收到了一个 UIKeyboardWillShowNotification 的 NSNotification 事件 谁能告诉我如何才能知道这个 NSNotification 是从哪里发送的?最好在 swift

我可以看到通知的名称,但看不到它的生成位置

NSNotificationCenter.defaultCenter().addObserverForName(nil,object: nil,queue: nil)                                                            
    {
      note in
      if(note.name.containsString("keyboard") ){
         print(note)
      }                                                                    
    }

与“从何处发送此 NSNotification”无关。重要的是 通知的含义以及 何时 发送通知。这意味着键盘即将在屏幕上显示动画,它在动画开始时发送。这意味着如果你想移动一些视图以使其不被键盘隐藏,你可以收听此通知以了解何时移动它们。如果您想要使该动作与键盘动画同步,您可以在收到此通知时开始动画。

综上所述,如果您真的想了解“从哪里”,您可以在通知处理程序中放置一个断点,然后在调用通知处理程序时查看堆栈跟踪。示例:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(forName: .UIKeyboardWillShow, object: nil, queue: nil) { (note) in
            // put a breakpoint on the next line
            Swift.print(note)
        }
    }


}

我在故事板中放入了一个文本字段。我 运行 Xcode 8.2 beta 2 模拟器中的应用程序,模拟 iPhone 7 Plus 运行 iOS 10.2。我点击了文本字段。这是断点处的堆栈跟踪:

#0  0x0000000106b4a184 in ViewController.(viewDidLoad() -> ()).(closure #1) at /Users/mayoff/TestProjects/test/test/ViewController.swift:18
#1  0x0000000106b4a2a9 in thunk ()
#2  0x0000000106c380da in -[__NSObserver _doit:] ()
#3  0x0000000109db45ec in __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ ()
#4  0x0000000109db44eb in _CFXRegistrationPost ()
#5  0x0000000109db4252 in ___CFXNotificationPost_block_invoke ()
#6  0x0000000109d77282 in -[_CFXNotificationRegistrar find:object:observer:enumerator:] ()
#7  0x0000000109d7631b in _CFXNotificationPost ()
#8  0x0000000106bf081b in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#9  0x000000010806ee76 in -[UIInputWindowController postStartNotifications:withInfo:] ()
#10 0x0000000108071117 in __77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke.871 ()
#11 0x00000001076b4432 in +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] ()
#12 0x00000001076b48bf in +[UIView(UIViewAnimationWithBlocks) _animateWithDuration:delay:options:animations:start:completion:] ()
#13 0x0000000108070b44 in -[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:] ()
#14 0x0000000108078b2b in -[UIInputWindowController setInputViewSet:] ()
#15 0x0000000108070158 in -[UIInputWindowController performOperations:withAnimationStyle:] ()
#16 0x0000000107ce335d in -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] ()
#17 0x0000000107814837 in -[UIResponder(UIResponderInputViewAdditions) reloadInputViews] ()
#18 0x000000010781144e in -[UIResponder becomeFirstResponder] ()
#19 0x00000001076aaafb in -[UIView(Hierarchy) becomeFirstResponder] ()
#20 0x00000001081097c8 in -[UITextField becomeFirstResponder] ()
#21 0x0000000107b42f4f in -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] ()
#22 0x0000000107b46767 in -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] ()
#23 0x0000000107b34431 in -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] ()
#24 0x0000000107b3c1d0 in _UIGestureRecognizerSendTargetActions ()
#25 0x0000000107b39c9f in _UIGestureRecognizerSendActions ()
#26 0x0000000107b38f2b in -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] ()
#27 0x0000000107b24fa6 in _UIGestureEnvironmentUpdate ()
#28 0x0000000107b249eb in -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] ()
#29 0x0000000107b23bce in -[UIGestureEnvironment _updateGesturesForEvent:window:] ()
#30 0x000000010766ab6d in -[UIWindow sendEvent:] ()
#31 0x00000001076178fb in -[UIApplication sendEvent:] ()
#32 0x0000000107e0374d in __dispatchPreprocessedEventFromEventQueue ()
#33 0x0000000107dfc483 in __handleEventQueue ()
#34 0x0000000109dbb761 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#35 0x0000000109da098c in __CFRunLoopDoSources0 ()
#36 0x0000000109d9fe76 in __CFRunLoopRun ()
#37 0x0000000109d9f884 in CFRunLoopRunSpecific ()
#38 0x000000010bd50a6f in GSEventRunModal ()
#39 0x00000001075f9bb8 in UIApplicationMain ()
#40 0x0000000106b4ba8f in main at /Users/mayoff/TestProjects/test/test/AppDelegate.swift:12
#41 0x000000010adc668d in start ()
#42 0x000000010adc668d in start ()

从这里我们可以看到 UIInputWindowController 的实例(UIKit 私有的 class)似乎负责动画键盘的位置,并发布通知。