无法找到NSNotification的发件人

时间:2016-11-29 14:04:17

标签: ios swift nsnotification

我正在接收UIKeyboardWillShowNotification的NSNotification事件任何人都可以告诉我如何才能找到这个NSNotification发送的位置?最好是快速的

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

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

1 个答案:

答案 0 :(得分:1)

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

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

#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 ()

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

UIInputWindowController

由此我们可以看到public int Imti(char sim) { return Rn[sim]; } public void kiek() { for (int i = 0; i < eil.Length; i++) { if (('a' <= eil[i] && eil[i] <= 'z') || ('A' <= eil[i] && eil[i] <= 'Z')) Rn[eil[i]]++; } } static void Spausdinti(string fv, RaidziuDazniai eil) { using (var fr = File.CreateText(fv)) { string[] lines = File.ReadAllLines(fv, Encoding.GetEncoding(1257)); for (char sim = 'a'; sim <= 'z'; sim++) fr.WriteLine("{0,3:c} {1,4:d} |{2,3:c} {3,4:d}", sim, eil.Imti(sim), Char.ToUpper(sim), eil.Imti(Char.ToUpper(sim))); } } static void Dazniai(string fv, RaidziuDazniai eil) { using (StreamReader reader = new StreamReader(fv)) { string line; while ((line = reader.ReadLine()) != null) { eil.eil = line; eil.kiek(); } } (UIKit专用的一个类)的实例似乎负责动画键盘的位置,并发布通知。