简体   繁体   English

使用DecimalPad时如何关闭键盘

[英]How to dismiss keyboard when using DecimalPad

I have a UITableView with a custom cell that has a TextField . 我有一个带有TextField的自定义单元格的UITableView I have the DecimalPad comes up, and as we all know, there is no done key. 我有DecimalPad ,我们都知道,没有完成密钥。 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). 通过处理TouchesEnded事件然后检查TextField是否是第一个响应者然后检查TextField是否是第一个响应者,我之前已经解决了这种类型的问题,然后我在正常的UIView上有一个“仅十进制”文本字段,然后它会重新签名,但如果是技术现在可以工作然后我无法TouchesEnded我应该使用TouchesEndedUIView ,所有内容都出现在, UITableView ,Cell,CellControler,TextField ......我想我已经尝试了一切)。

I'm hoping there's another, cleaner way of dealing with this. 我希望有另一种更清洁的方法来解决这个问题。

Anyone? 任何人?

I think David has the best idea - here is some Monotouch code to get you started. 我认为大卫有最好的主意 - 这里有一些Monotouch代码可以让你入门。 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;
    }
}

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. 如果您的目标是iOS 4.0或更高版本,则可以创建一个inputAccessoryView,其中包含一个Done按钮以附加到键盘上,该键盘将在点按时关闭键盘。 Here is an example from the documentation on creating a simple inputAccessoryView. 以下是有关创建简单inputAccessoryView的文档中的示例。

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 . 在Interface Builder中,将View的类更改为UIControl This is a subclass of UIView , so your program will work the same way, but you also get the standard touch events. 这是UIView的子类,因此您的程序将以相同的方式工作,但您也可以获得标准的触摸事件。

From here it's simple, create a method for the Touch Down event: 从这里开始很简单,为Touch Down事件创建一个方法:
[numberField resignFirstResponder]

Of course it might be slightly different with MonoTouch -- unfortunately I don't know much about it, but wanted to help. 当然,与MonoTouch可能略有不同 - 遗憾的是我对此并不了解,但想要提供帮助。
Hopefully you can use the concept, and modify your code accordingly. 希望您可以使用该概念,并相应地修改您的代码。

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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM