使用 DecimalPad 时如何关闭键盘

发布于 2024-10-28 23:39:44 字数 415 浏览 7 评论 0原文

我有一个 UITableView ,其中包含一个带有 TextField 的自定义单元格。我出现了DecimalPad,众所周知,没有完成键。当我在普通 UIView 上有一个“仅十进制”文本字段时,我之前通过处理 TouchesEnded 事件,然后检查 TextField 是否为第一响应者,如果是这样,它将辞职,但如果该技术现在可以工作,那么我无法弄清楚我应该使用谁的 TouchesEndedUIView一切都呈现在 UITableView、Cell、CellControler、TextField 上。我想我已经尝试了一切)。

我希望有另一种更干净的方法来处理这个问题。

有人吗?

I have a UITableView with a custom cell that has a TextField. I have the DecimalPad comes up, and as we all know, there is no done key. I previously had resolved this type of issue when I had a "Decimal only" textfield on a normal UIView by handling the TouchesEnded event and then checking to see if the TextField was the first responder and if so, it would then resign, but if that technique could work now then I'm not able to figure out who's TouchesEnded I should be using (The UIView that everything is presented on, the UITableView, the Cell, the CellControler, the TextField.. I think I've tried everything).

I'm hoping there's another, cleaner way of dealing with this.

Anyone?

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

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

发布评论

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

评论(4

甜尕妞 2024-11-04 23:39:45

我认为大卫有最好的主意 - 这是一些 Monotouch 代码可以帮助您入门。您需要将其放入显示小数点的视图控制器中:

UIView dismiss;

public override UIView InputAccessoryView 
{
    get 
    {
        if (dismiss == null)
        {
            dismiss = new UIView(new RectangleF(0,0,320,27));
            dismiss.BackgroundColor = UIColor.FromPatternImage(new UIImage("Images/accessoryBG.png"));          
            UIButton dismissBtn = new UIButton(new RectangleF(255, 2, 58, 23));
            dismissBtn.SetBackgroundImage(new UIImage("Images/dismissKeyboard.png"), UIControlState.Normal);        
            dismissBtn.TouchDown += delegate {
                 textField.ResignFirstResponder();
            };
            dismiss.AddSubview(dismissBtn);
        }
        return dismiss;
    }
}

I think David has the best idea - here is some Monotouch code to get you started. You will need to put this in the View Controller where the decimal pad is being shown:

UIView dismiss;

public override UIView InputAccessoryView 
{
    get 
    {
        if (dismiss == null)
        {
            dismiss = new UIView(new RectangleF(0,0,320,27));
            dismiss.BackgroundColor = UIColor.FromPatternImage(new UIImage("Images/accessoryBG.png"));          
            UIButton dismissBtn = new UIButton(new RectangleF(255, 2, 58, 23));
            dismissBtn.SetBackgroundImage(new UIImage("Images/dismissKeyboard.png"), UIControlState.Normal);        
            dismissBtn.TouchDown += delegate {
                 textField.ResignFirstResponder();
            };
            dismiss.AddSubview(dismissBtn);
        }
        return dismiss;
    }
}
我不会写诗 2024-11-04 23:39:45

如果您的目标是 iOS 4.0 或更高版本,则可以创建 inputAccessoryView 包含一个附加到键盘的“完成”按钮,点击该按钮将关闭键盘。 这里是来自有关创建简单 inputAccessoryView 的文档。

If you're targeting iOS 4.0 or greater you can create an inputAccessoryView containing a Done button to attach to the keyboard that will dismiss the keyboard when tapped. Here is an example from the documentation on creating a simple inputAccessoryView.

冰雪梦之恋 2024-11-04 23:39:45

当用户点击背景时,您可以关闭它;我认为这是最直观的方式。

在 Interface Builder 中,将视图的类更改为 UIControl。这是 UIView 的子类,因此您的程序将以相同的方式工作,但您还可以获得标准触摸事件。

从这里开始很简单,为 Touch Down 事件创建一个方法:
[numberField resignFirstResponder]

当然,它可能与 MonoTouch 略有不同 - 不幸的是我对此了解不多,但希望提供帮助。
希望您可以使用这个概念,并相应地修改您的代码。

You could dismiss it when the user taps on the background; I think that's the most intuitive way.

In Interface Builder, change your View's class to UIControl. This is a subclass of UIView, so your program will work the same way, but you also get the standard touch events.

From here it's simple, create a method for the Touch Down event:
[numberField resignFirstResponder]

Of course it might be slightly different with MonoTouch -- unfortunately I don't know much about it, but wanted to help.
Hopefully you can use the concept, and modify your code accordingly.

几度春秋 2024-11-04 23:39:45

或者您可以在主视图中添加一些手势。
例如:

//Just initialise the gesture you want with action that dismisses your num pad
-(void)textFieldDidBeginEditing:(UITextField *)textField
{    
     UISwipeGestureRecognizer *swipeToHideNumPad = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideNumPad:)];
            swipeToHideNumPad.delegate = self;
            swipeToHideNumPad.direction = UISwipeGestureRecognizerDirectionDown;
            [swipeToHideNumPad setNumberOfTouchesRequired:1];

            [self.view addGestureRecognizer:swipeToHideNumPad];
}


//action
- (void)hideNumPad:(UIGestureRecognizer *)gestureRecognizer
{
    [self.amountTextField resignFirstResponder];
}

Or you may just add some gesture to your main view.
For example:

//Just initialise the gesture you want with action that dismisses your num pad
-(void)textFieldDidBeginEditing:(UITextField *)textField
{    
     UISwipeGestureRecognizer *swipeToHideNumPad = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideNumPad:)];
            swipeToHideNumPad.delegate = self;
            swipeToHideNumPad.direction = UISwipeGestureRecognizerDirectionDown;
            [swipeToHideNumPad setNumberOfTouchesRequired:1];

            [self.view addGestureRecognizer:swipeToHideNumPad];
}


//action
- (void)hideNumPad:(UIGestureRecognizer *)gestureRecognizer
{
    [self.amountTextField resignFirstResponder];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文