Ios UITextFieldDelegate不工作
我是Objective-C初学者,我做了一个猜谜游戏。游戏生成一个介于1和10之间的随机整数(含),用户必须猜出数字。然后游戏继续验证猜测是否正确,并提示“更高”或“更低” 这个游戏运行得很好。我包括一个代表,在使用UITextField时删除键盘。我不知道是什么问题我已经查看了所有的堆栈溢出,以了解如何做到这一点,但是所有的解决方案似乎都包括了我已有的解决方案Ios UITextFieldDelegate不工作,ios,objective-c,Ios,Objective C,我是Objective-C初学者,我做了一个猜谜游戏。游戏生成一个介于1和10之间的随机整数(含),用户必须猜出数字。然后游戏继续验证猜测是否正确,并提示“更高”或“更低” 这个游戏运行得很好。我包括一个代表,在使用UITextField时删除键盘。我不知道是什么问题我已经查看了所有的堆栈溢出,以了解如何做到这一点,但是所有的解决方案似乎都包括了我已有的解决方案 // GGameViewController.h #import <UIKit/UIKit.h> @interface
// GGameViewController.h
#import <UIKit/UIKit.h>
@interface GGameViewController : UIViewController<UITextFieldDelegate>
{
}
@end
// GGameViewController.m
#import "GGameViewController.h"
@interface GGameViewController ()
@property (weak, nonatomic) IBOutlet UITextField *inputTxtField;
@property (weak, nonatomic) IBOutlet UILabel *lblHints;
@property int r;
- (IBAction)btnGuess:(id)sender;
@end
@implementation GGameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.inputTxtField.delegate = self;
self.r = (arc4random() % 10) + 1;//Got this from stackoverflow at http://stackoverflow.com/questions/510367/how-do-i-generate-random-numbers-on-the-iphone
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL) textfieldShouldReturn: (UITextField *)textField{
[textField resignFirstResponder ];
return YES;
}
- (IBAction)btnGuess:(id)sender {
int guess = [self.inputTxtField.text intValue];
NSString *response = @" ";
if(guess == self.r){
response = @"You got it!";
self.inputTxtField.userInteractionEnabled = NO;//Got this from stackoverflow at http://stackoverflow.com/questions/5947965/ios-uitextfield-disabled
}
else if(guess < self.r)
response = @"Higher";
else if(guess > self.r)
response = @"Lower";
self.lblHints.text = [[NSString alloc] initWithFormat: @"%@",response];
}
@end
//GGameViewController.h
#进口
@接口GGameViewController:UIViewController
{
}
@结束
//GGameViewController.m
#导入“GGameViewController.h”
@接口GGameViewController()
@属性(弱,非原子)IBOutlet UITextField*InputXTField;
@属性(弱、非原子)IBUILabel*lblHints;
@属性int r;
-(iAction)b语言:(id)发送方;
@结束
@AMEViewController的实现
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
self.inputxtfield.delegate=self;
self.r=(arc4random()%10)+1;//从位于http://stackoverflow.com/questions/510367/how-do-i-generate-random-numbers-on-the-iphone
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(BOOL)textField应返回:(UITextField*)textField{
[textField resignFirstResponder];
返回YES;
}
-(iAction)b语言:(id)发送方{
int guess=[self.inputxtfield.text intValue];
NSString*响应=@“”;
if(guess==self.r){
回答=@“你明白了!”;
self.inputXtfield.userInteractionEnabled=NO;//从位于http://stackoverflow.com/questions/5947965/ios-uitextfield-disabled
}
else if(猜测self.r)
答复=@“较低”;
self.lblHints.text=[[NSString alloc]initWithFormat:@“%@”,response];
}
@结束
如果您从编辑器复制了此代码,并且委托回调的签名确实是
-(BOOL)textField应该返回:(UITextField*)textField{
那么您的问题是区分大小写的问题。textfield
中的“f”应该大写,如下所示:
-(BOOL)textField应该返回:(UITextField*)textField{
尝试[self.view endEditing:YES];
而不是[textField resignFirstResponder]
。看起来都不错。顺便问一句……你使用的键盘类型/风格是什么?它有完成/返回类型按钮吗?如果你使用的是数字键盘
,那么默认情况下它不会有返回按钮。你有没有将inputExtField
与IB中相应的UITextField
连接起来?@GlennRay:嗨,我试过了,现在仍然有不行。@staticVoidMan:我正在使用一个带返回按钮的键盘。非常感谢,sixstringtheory!这就解决了问题。为什么会出现这个问题?TextField不应该返回一个自定义方法,可以按我希望的方式拼写吗?问题只是大小写。Objective-C总是区分大小写,所以textFieldShouldReturn
是与textfield不同,后者是为UITextField定义的委托方法。很高兴它现在可以工作了!