Ios UITextField(仅带数字)&&引用;“完成”;在iPhone的键盘上

Ios UITextField(仅带数字)&&引用;“完成”;在iPhone的键盘上,ios,keyboard,uitextfield,numeric,Ios,Keyboard,Uitextfield,Numeric,我正在学习obj-c,给自己一个简单的任务,创建一个应用程序,它有2个uitextfields,标签和按钮(按钮汇总了2个数字,并在标签中显示结果)。我制作了这个应用程序,但我还没有弄清楚如何使文本字段与“完成”按钮和“仅数字”一起交互 下面是我的代码: #import UIKit/UIKit.h @interface ViewController : UIViewController @property IBOutlet UILabel *label; @property IBOutlet

我正在学习obj-c,给自己一个简单的任务,创建一个应用程序,它有2个
uitextfields
标签和
按钮(按钮汇总了2个数字,并在标签中显示结果)。我制作了这个应用程序,但我还没有弄清楚如何使
文本字段
与“完成”按钮和“仅数字”一起交互

下面是我的代码:

#import UIKit/UIKit.h

@interface ViewController : UIViewController

@property IBOutlet UILabel *label;
@property IBOutlet UITextField *textField1;
@property IBOutlet UITextField *textField2;
@property NSInteger textFieldInt1, textFieldInt2;
@property NSString *textFieldStr1, *textFieldStr2;

-(IBAction)button:(id)sender;
-(IBAction)hideKeyboard:(id)sender;

@end


#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

@synthesize label, textField1, textField2, textFieldInt1, textFieldInt2, textFieldStr1, textFieldStr2;

-(IBAction)button:(id)sender {
    textFieldInt1=[textField1.text integerValue];
    textFieldInt2=[textField2.text integerValue];
    label.text=[NSString stringWithFormat: @"%d", textFieldInt1+textFieldInt2];
    [self.view endEditing:YES];
}



- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    [self.textField1 setReturnKeyType:UIReturnKeyDone];
    [self.textField2 setReturnKeyType:UIReturnKeyDone];

}

-(IBAction)hideKeyboard:(id)sender
{
    [self.view endEditing:YES];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range     replacementString:(NSString *)string {
    if(![string intValue] && ![string isEqualToString:@""] && ![string isEqualToString:@"0"])
    {
    return NO;
    } else {
    return YES;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    [super touchesBegan:touches withEvent:event];
}


@end
我还(在故事板中)将
textFields
连接到
hideKeyboard(didenonexit)
,将
按钮
连接到
按钮(touchupind)
和所有插座(
textFields
按钮
标签

问题是:
仅限数字
输入不适用于
完成
。如果我使用
delegate
(故事板中连接到两个
textfields
)的新引用插座,我只能在
textfields
中输入
数字,但
done
按钮不起作用(当我在iPhone上按下它时,不会发生任何事情)。如果我不使用
delegate
我得到
所有字符
输入,但
完成
工作正常(当我按下它时,iPhone键盘隐藏)


有什么建议我在这里可能做错了什么吗?

实施UITextFieldDelegate方法:

// This is how we get rid of the keyboard.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    //Do whatever you need to to save etc...
    [textField resignFirstResponder];
    return NO;
}
这会告诉键盘在按下“完成”按钮时隐藏。 确保将视图控制器设置为textfields的委托: myTextField.delegate=self;
(可能是在viewDidLoad中)

这是一条评论,回应了某人发布的评论,然后被删除。因为原来的评论不见了,我不能删除这个评论,你会得到这个垃圾评论…太好了。我为我的英语感到抱歉,可能我没有把我的问题说清楚。我有“完成”按钮,它工作正常(隐藏键盘),但不使用“仅数字”输入限制。这就是我无法解决的问题。非常感谢!很好用!我已将我的方法(IBAction)hideKeyboard:(id)sender{[self.view endEditing:YES];}替换为您的方法,并将两个文本字段与“delegate”(新引用)关联。