Ios Xcode键盘登录屏幕

Ios Xcode键盘登录屏幕,ios,xcode,uikeyboard,Ios,Xcode,Uikeyboard,我正在开发一个具有可编辑功能的键盘的应用程序。在mainViewController.m文件中,我可以使用以下代码行正确隐藏键盘: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; [super touchesBegan:touches withEvent:event]; } 完成应用程序的主要功能后,我创建了一个简单的登录屏幕。但是,与上面相同的代码在l

我正在开发一个具有可编辑功能的键盘的应用程序。在mainViewController.m文件中,我可以使用以下代码行正确隐藏键盘:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
[super touchesBegan:touches withEvent:event];
}
完成应用程序的主要功能后,我创建了一个简单的登录屏幕。但是,与上面相同的代码在loginScreen.m文件中不起作用

@implementation LoginScreen



-(void)viewDidLoad {
        [super viewDidLoad];
}



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

    #pragma mark - View lifecycle

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction)btnLoginRegisterTapped:(UIButton*)sender
{
    //form fields validation
    if (fldUsername.text.length < 4 || fldPassword.text.length < 4) {
        [UIAlertView error:@"Enter username and password over 4 chars each."];
        return;
    }

//salt the password
NSString* saltedPassword = [NSString stringWithFormat:@"%@%@", fldPassword.text, kSalt];

// Check for name/pw length

//prepare the hashed storage
NSString* hashedPassword = nil;
unsigned char hashedPasswordData[CC_SHA1_DIGEST_LENGTH];

//hash the pass
NSData *data = [saltedPassword dataUsingEncoding: NSUTF8StringEncoding];
if (CC_SHA1([data bytes], [data length], hashedPasswordData)) {
    hashedPassword = [[NSString alloc] initWithBytes:hashedPasswordData length:sizeof(hashedPasswordData) encoding:NSASCIIStringEncoding];
} else {
    [UIAlertView error:@"Password can't be sent"];
    return;
}

//check whether it's a login or register
NSString* command = (sender.tag==1)?@"register":@"login";
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:
                              command, @"command",
                              fldUsername.text, @"username",
                              hashedPassword, @"password",
                              nil];

//make the call to the web API
[[API sharedInstance] commandWithParams:params
                           onCompletion:^(NSDictionary *json) {


                               //result returned
                               NSDictionary* res = [[json objectForKey:@"result"] objectAtIndex:0];

                               if ([json objectForKey:@"error"]==nil && [[res objectForKey:@"IdUser"] intValue]>0) {
                                   [[API sharedInstance] setUser: res];
                                   [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

                                   //show message to the user
                                   [[[UIAlertView alloc] initWithTitle:@"Logged in"
                                                               message:[NSString stringWithFormat:@"Welcome %@",[res objectForKey:@"username"] ]
                                                              delegate:nil 
                                                     cancelButtonTitle:@"Close" 
                                                     otherButtonTitles: nil] show];

                               } else {
                                   //error
                                   [UIAlertView error:[json objectForKey:@"error"]];
                               }



                           }];



}

@end
@实现登录屏幕
-(无效)viewDidLoad{
[超级视图下载];
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
[自视图编辑:是];
[超级触摸开始:触摸事件:事件];
}
#pragma标记-视图生命周期
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
//对于支持的方向返回YES
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
-(iAction)btnLoginRegisterTapped:(UIButton*)发送器
{
//表单字段验证
if(fldUsername.text.length<4 | | fldPassword.text.length<4){
[UIAlertView错误:@“输入用户名和密码,每个超过4个字符。”;
返回;
}
//修改密码
NSString*saltedPassword=[NSString stringWithFormat:@“%@%@”,fldPassword.text,kSalt];
//检查名称/pw长度
//准备散列存储
NSString*hashedPassword=nil;
无符号字符哈希密码数据[CC_SHA1_DIGEST_LENGTH];
//散列通行证
NSData*data=[saltedPassword dataUsingEncoding:NSUTF8StringEncoding];
if(CC_SHA1([数据字节],[数据长度],哈希密码数据)){
hashedPassword=[[NSString alloc]initWithBytes:hashedPasswordData长度:sizeof(hashedPasswordData)编码:NSASCIIStringEncoding];
}否则{
[UIAlertView错误:@“无法发送密码”];
返回;
}
//检查是登录还是注册
NSString*命令=(发送者.标记==1)?@“注册”:@“登录”;
NSMutableDictionary*参数=[NSMutableDictionary Dictionary WithObjectsAndKeys:
命令,@“命令”,
fldUsername.text,@“用户名”,
hashedPassword,@“password”,
零];
//调用web API
[[API sharedInstance]命令带参数:参数
onCompletion:^(NSDictionary*json){
//返回的结果
NSDictionary*res=[[json objectForKey:@“结果”]objectAtIndex:0];
如果([json objectForKey:@“error”]==nil&&[[res objectForKey:@“IdUser”]intValue]>0){
[[API sharedInstance]设置用户:res];
[self.presentingViewController dismissViewControllerAnimated:YES完成:nil];
//向用户显示消息
[[[UIAlertView alloc]initWithTitle:@“已登录”
消息:[NSString stringWithFormat:@“Welcome%@”,[res objectForKey:@“username”]]
代表:无
取消按钮:@“关闭”
其他按钮:无]显示];
}否则{
//错误
[UIAlertView错误:[json objectForKey:@“错误”];
}
}];
}
@结束

如果我找到了修复方法,我会将其添加到这里,但我不确定是什么原因导致了此问题,请提供任何帮助。

您可以使用点击手势隐藏键盘

 -(void)viewDidLoad {
     [super viewDidLoad];
     UITapGestureRecognizer *tapGestureRecognizer =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapReceived:)];
    [self.view addGestureRecognizer:tapGestureRecognizer];
}



-(void)tapReceived:(UITapGestureRecognizer *)tapGestureRecognizer {
[self.view endEditing:YES];
[yourtextfield resignFirstResponder];

}

@扎夫:谢谢你的回复。我知道Xcode没有运行该应用程序,因为我有各种iOS设备进行测试。一个简单的“我想你可能在问题标题中有错误”可能会更有帮助。返回到原始标题。