我正在接收一个UIKeyboardWillShowNotification的NSNotification事件。有人能告诉我如何才能从发送此NSNotification的位置找到它吗?最好是斯威夫特
我可以看到通知的名称,但看不到它是从哪里生成的

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.2beta 2模拟器中运行了这个应用程序,模拟运行iOS 10.2的iPhone 7 Plus。我点击了文本字段。以下是断点处的堆栈跟踪:
#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的私有类)的一个实例似乎负责设置键盘位置的动画,并发布通知。

关于ios - 找不到NSNotification的发件人,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40867842/

10-08 20:57