Swift 2 addObserver用于带有object参数的特定textField [英] Swift 2 addObserver for specific textField with the object parameter

查看:325
本文介绍了Swift 2 addObserver用于带有object参数的特定textField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,addObserver方法的object参数是您要从中接收通知的对象.大多数情况下,我将其视为nil(我认为这是因为所有对象都需要指定类型的通知).在我的特定情况下,我在屏幕顶部和屏幕底部都有一个文本字段,并且我希望视图仅在用户点击底部文本字段而不是顶部文本字段时向上移动.所以我在viewWillAppear

To my understanding the object parameter of the addObserver method is the object you want to receive notifications from. Most of the time I see it as nil (I assume this is because notifications of the specified type are wanted from all objects). In my particular case I have a text field at the top of the screen and at the bottom of the screen and I want the view to move up only when the user taps the bottom text field, not the top one. So I call the following method in viewWillAppear

func subscribeToKeyboardNotifications() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: self.bottomTextField)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: self.bottomTextField)
}

keyboardWillShow:keyboardWillHide:选择器调用用于重新定位视图框架的方法.我尝试将object参数保留为nil,但这将同时接收来自两个文本字段的通知.我尝试将object参数设置为self.bottomTextField(如上所示),但是没有从任何一个文本字段接收通知.我的问题是双重的.首先,我是否正确理解了addObserver方法(尤其是object参数),其次,为什么它没有注册来自self.bottomTextField的通知.谢谢!

the keyboardWillShow: and keyboardWillHide: selectors call methods that do the repositioning of the view's frame. I tried leaving the object parameter to be nil but that would receive notifications from both text fields. I tried setting the object parameter to self.bottomTextField (as seen above) but that doesn't receive notifications from either text field. My question is twofold. First, am I understanding the addObserver method correctly (especially the object parameter) and second, why is it not registering notifications from self.bottomTextField. Thanks!

解决方案: 我知道这不是我要问的确切问题的解决方案,但是当仅单击底部文本字段时,最终我做了什么操作以使视图向上移动:

Solution: I know this isn't the solution to the exact question I was asking but what I did in the end to make the view move up when only clicking on the bottom text field was the following:

func subscribeToKeyboardNotifications() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}

,然后在keyboardWillShow:方法中,我有:

and then in the keyboardWillShow: method I have:

func keyboardWillShow(notification: NSNotification) {
    if bottomTextField.editing { // only reset frame's origin if editing from the bottomTextField
        view.frame.origin.y -= getKeyboardHeight(notification)
    }
}

希望有帮助!

推荐答案

首先,我是否正确理解了addObserver方法(尤其是对象参数)

First, am I understanding the addObserver method correctly (especially the object parameter)

是的,您已经知道了.指定nil意味着无论发送哪个对象,您都将收到通知.提供指向对象的指针意味着您正在观察来自该特定对象的通知.

Yes, you've got it. Specifying nil means that you'll get the notification regardless of which object sent it; providing a pointer to an object means that you're observing the notification from that specific object.

第二,为什么它不注册来自self.bottomTextField的通知

second, why is it not registering notifications from self.bottomTextField

您看到的是错误的通知. UITextField从不发送UIKeyboardWillShowNotification-这是来自窗口的通知.如果为object参数指定nil,则可以从发送它的任何对象(包括窗口)获取通知.但是,当您将文本字段指定为对象参数时,您根本不会收到任何通知,因为文本字段不会发送该通知.您应该改为观察UITextFieldTextDidBeginEditingNotificationUITextFieldTextDidEndEditingNotification,这是UITextField发送的通知.

You're observing the wrong notification. UITextField never sends UIKeyboardWillShowNotification -- that's a notification that comes from the window. If you specify nil for the object parameter, then you get the notification from any object that sends it, including the window. But when you specified the text field as the object parameter, you didn't get any notification at all because text fields don't send that notification. You should instead observe UITextFieldTextDidBeginEditingNotification and UITextFieldTextDidEndEditingNotification, which are the notifications that UITextField sends.

这篇关于Swift 2 addObserver用于带有object参数的特定textField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆