2015-06-03 75 views
0

我正在研究具有可编辑功能的键盘功能的应用程序。在mainViewController.m文件,我能够正确地隐藏键盘与下面的代码行:Xcode关闭键盘登录屏幕

- (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)[email protected]"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 

如果我弄明白了修复,我会在这里,但我不知道是什么原因造成这个问题,任何帮助,将不胜感激。

+0

@zaph谢谢您的答复。我知道Xcode不会运行该应用程序,因为我有各种iOS设备进行测试。 –

+0

一个简单的“我认为你可能在问题标题中有错误”可能会更有帮助。 –

+0

返回原始标题。 – zaph

回答

0

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

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

}