2016-11-29 65 views
0

我正在接收UIKeyboardWillShowNotification的NSNotification事件任何人都可以告訴我如何從NSNotification的發送地點找出來?最好是在迅速找不到NSNotification的發件人

我可以看到通知的名稱,但不能從那裏產生

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

您是否檢查過note.object? –

+1

看看'NSNotification'的文檔。屬性'object':'這通常是發佈此通知的對象。它可能是零。「它可以工作,但並不強制要求發佈通知的那個人。 – Larme

+0

您可以使用object或userInfo來達到您的目的。 –

回答

1

這是「從那裏該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模擬器中運行該應用程序,模擬運行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)似乎是負責動畫鍵盤的位置,並張貼通知。