如何找出 UITextField 导致 UIKeyboardWillShowNotification 的原因?

发布于 2024-08-08 23:53:06 字数 1412 浏览 7 评论 0原文

我试图在我的应用程序中使用自定义键盘,但在尝试将其限制为一个特定的 UITextField 时遇到问题。

我的代码基于 这个 Xcode 项目 (最初发现于此博客)。该代码将自定义 UIButton(代表“小数点”)添加到 UIKeyboardTypeNumberPad 键盘视图中。它通过订阅 UIKeyboardWillShowNotification 并在键盘出现时修改键盘来实现。

该 Xcode 项目运行良好,但当我添加额外的 UITextField 时,自定义键也会放入该文本字段的键盘中,即使我为该文本字段选择了完全不同的键盘类型。

我尝试注册以仅查看来自一个特定 UITextField 的 UIKeyboardWillShowNotification 通知,但这似乎不起作用:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:exampleViewController.textField];

我还尝试检查传递给 KeyboardWillShow 的 NSNotification 内的对象,但不幸的是它指的是键盘,而不是 UI 控件这导致键盘出现。

2009-10-21 19:50:22.205 Example[6302:207] NSConcreteNotification 0x321ebd0 {name = UIKeyboardWillShowNotification; userInfo = {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0.300000011920929;
UIKeyboardBoundsUserInfoKey = NSRect: {{0, 0}, {320, 216}};
UIKeyboardCenterBeginUserInfoKey = NSPoint: {160, 588};
UIKeyboardCenterEndUserInfoKey = NSPoint: {160, 372};

我是否

误解了 addObserver 接口?肯定有一种方法可以订阅来自特定 UI 控件的通知吗?

有人对如何实现这一目标有任何其他建议吗?

I am trying to use a customized keyboard in my application, but I am hitting problems when trying to restrict it to one particular UITextField.

I based my code on this Xcode project (originally found on this blog). That code adds a custom UIButton (representing a 'decimal point') into the UIKeyboardTypeNumberPad keyboard view. It does it by subscribing to UIKeyboardWillShowNotification and modifying the keyboard when it appears.

That Xcode project works great, but when I add an extra UITextField, the custom key gets put into the keyboard for that text field too, even though I have selected a completely different keyboard type for that text field.

I attempted to register to only see UIKeyboardWillShowNotification notifications from the one particular UITextField, but that doesn't seem to work:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:exampleViewController.textField];

I also tried to inspect the object inside the NSNotification passed to keyboardWillShow, but unfortunately it refers to the keyboard, not the UI control that caused the keyboard to appear.

2009-10-21 19:50:22.205 Example[6302:207] NSConcreteNotification 0x321ebd0 {name = UIKeyboardWillShowNotification; userInfo = {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0.300000011920929;
UIKeyboardBoundsUserInfoKey = NSRect: {{0, 0}, {320, 216}};
UIKeyboardCenterBeginUserInfoKey = NSPoint: {160, 588};
UIKeyboardCenterEndUserInfoKey = NSPoint: {160, 372};

}}

Am I misunderstanding the addObserver interface? Surely there must be a way to subscribe to notifications from a particular UI control?

Has anybody got any other suggestions on how to achieve this?

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

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

发布评论

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

评论(4

仙气飘飘 2024-08-15 23:53:06

无法使上述任何一项工作。必须手动完成:

if ([companyName isFirstResponder]) {
    // ...............
}
else if ([notes isFirstResponder]) {
    //.............
    }
}

Couldn't get any of the above to work. Had to do it manually:

if ([companyName isFirstResponder]) {
    // ...............
}
else if ([notes isFirstResponder]) {
    //.............
    }
}
命比纸薄 2024-08-15 23:53:06

您可以编写一个 UIView 类别方法来查找第一响应者。

- (UIView *)firstResponder
{
    if ([self isFirstResponder])
    {
        return self;
    }

    for (UIView *view in self.subviews)
    {
        UIView *firstResponder= [view firstResponder];
        if (firstResponder)
        {
            return firstResponder;
        }
    }

    return nil;
}

然后在你的 - (void)keyboardWillShow:(NSNotification *)notification 方法中你可以像这样使用它

  UITextField *textField = (UITextField *)[self firstResponder];

You can write a UIView category method to find the first responder.

- (UIView *)firstResponder
{
    if ([self isFirstResponder])
    {
        return self;
    }

    for (UIView *view in self.subviews)
    {
        UIView *firstResponder= [view firstResponder];
        if (firstResponder)
        {
            return firstResponder;
        }
    }

    return nil;
}

Then in your - (void)keyboardWillShow:(NSNotification *)notification method you can use it like this

  UITextField *textField = (UITextField *)[self firstResponder];
又怨 2024-08-15 23:53:06

该项目效果很好,但是当我添加额外的 UITextField 时,自定义键也会放入该文本字段的键盘中。

您可以为 UITextField 设置键盘类型例如:

[myTextField setKeyboardType:UIKeyboardTypeDefault];

或:

myTextField.keyboardType = UIKeyboardTypeDefault;

UITextInputTraits Protocol 的 Xcode 帮助中搜索 UIKeyboardType 常量列表。

That project works great, but when I add an extra UITextField, the custom key gets put into the keyboard for that text field too.

You can set the keyboard type for a UITextField instance, e.g.:

[myTextField setKeyboardType:UIKeyboardTypeDefault];

or:

myTextField.keyboardType = UIKeyboardTypeDefault;

Search the Xcode help on UITextInputTraits Protocol for a list of UIKeyboardType constants.

-黛色若梦 2024-08-15 23:53:06

@berilcetin 答案的快速版本。

extension UIView {
  var firstResponder: UIView? {
    if self.isFirstResponder {
        return self
    }
    return getSubView(views: self.subviews)
  }

  private func getSubView(views: [UIView]) -> UIView? {

    guard subviews.count > 0 else {
        return nil
    }
    if let responder = views.filter({ $0.isFirstResponder }).first {
        return responder
    }
    for responder in views {
        if let view = getSubView(views: responder.subviews) {
            return view
        }
    }
    return .none
 }
}

用法

yourView.firstResponder 作为? UITextField

如果您正在使用 UITableViewCell

for cell in tableView.visibleCells {
        if let view = cell.contentView.firstResponder as? UITextField {
            // view ...

            break
        }
    }

A swift version of @berilcetin's answer.

extension UIView {
  var firstResponder: UIView? {
    if self.isFirstResponder {
        return self
    }
    return getSubView(views: self.subviews)
  }

  private func getSubView(views: [UIView]) -> UIView? {

    guard subviews.count > 0 else {
        return nil
    }
    if let responder = views.filter({ $0.isFirstResponder }).first {
        return responder
    }
    for responder in views {
        if let view = getSubView(views: responder.subviews) {
            return view
        }
    }
    return .none
 }
}

usage

yourView.firstResponder as? UITextField.

if you are working on UITableViewCell

for cell in tableView.visibleCells {
        if let view = cell.contentView.firstResponder as? UITextField {
            // view ...

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