如何指定通知发送者? (可可触感)

发布于 2024-07-15 20:23:13 字数 422 浏览 3 评论 0原文

我能找到的所有设置通知的示例如下所示:

[nc addObserver:self 选择器:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];

其中 object 始终被设置为 nil。 object 是 notificationSender,在文档中是这样定义的:

“观察者想要接收其通知的对象;也就是说,只有该发送者发送的通知才会传递给观察者。当 nil 时,通知中心不使用通知的发送者决定是否将其传递给观察者。”

我只想接收一个特定文本视图的键盘通知,所以我想告诉 addObserver,但我不知道为该对象提供什么。 我尝试了绑定到我感兴趣的文本视图的出口变量,但这只是导致我根本没有收到任何通知。

有什么建议么?

All of the examples I can find of setting up a notification look like this:

[nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];

where object is always being set to nil. object is the notificationSender, which is defined thusly in the documentation:

"The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer. When nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer."

I want to receive the keyboard notification only for one particular text view, so I want to tell addObserver that, but I don't know what to give it for the object. I tried the outlet variable that's bound to the text view I'm interested in, but that just resulted in my getting no notifications at all.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗼ふ静 2024-07-22 20:23:13

UIKeyboardWillShowNotification 来自您的窗口实例,并且出于所有意图和目的,它是一个“系统”通知。 键盘要么显示,要么不显示,它并不是真正与特定控件相关的东西。

如果您想在用户输入特定文本字段时执行某些操作,您可能应该在文本字段的委托中进行控制。

The UIKeyboardWillShowNotification is coming from your window instance, and for all intents and purposes is a "system" notification. The keyboard is either showing or not showing, it's not really something that is tied to a specific control.

If you want to do something when a user enters a specific text field, you should probably control that in the text field's delegate instead.

时光倒影 2024-07-22 20:23:13

在这种情况下,您无法仅获取一个特定文本视图的通知,因为 UIKeyboardWillShowNotification 的文档显示“通知对象为零”。

如果您的特定文本视图是FirstResponder,则必须检查您的keyboardWillShow impl。

In this case you can not get the notification only for one particular text view, as the docu for UIKeyboardWillShowNotification says "The notification object is nil.".

You have to check in your keyboardWillShow impl if your particular text view isFirstResponder.

月竹挽风 2024-07-22 20:23:13

我有两个相互竞争的需求 - 我必须使用键盘通知,因为我需要获取键盘高度,这似乎只能通过这种方式获得,但我还需要知道我所在的文本视图,这意味着还要使用 textViewDidBeginEditing代表。 经过一番折腾后,我最终求助于在通知方法中获取键盘高度并将其存储在实例变量中,然后可以在委托方法中使用该变量(我需要向上滚动视图,以便底部文本视图不是主要的)当他们开始打字时隐藏在键盘下方)。 有点不优雅,但确实有效。

感谢您的指点!

I had two competing needs - I had to use the keyboard notification because I needed to get the keyboard height, which appears to be only available that way, but I also needed to know which text view I was in, which meant also using the textViewDidBeginEditing delegate. After much messing around I finally resorted to getting the keyboard height in the notification method and storing it in an instance variable, which was then available to use in the delegate method (I need to scroll the view up so the bottom text view is not mostly hidden under they keyboard when they start typing). A bit inelegant, but it works.

Thanks for the pointers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文