ios - Xcode Dismiss Keyboard Login Screen -
i'm working on application has functioning keyboard editing capabilities. in mainviewcontroller.m file, able hide keyboard following line of code:
- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { [self.view endediting:yes]; [super touchesbegan:touches withevent:event]; }
after completing main functionality of application, i've created simple login screen. however, same code above not working in loginscreen.m file.
@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 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 , password on 4 chars each."]; return; } //salt password nsstring* saltedpassword = [nsstring stringwithformat:@"%@%@", fldpassword.text, ksalt]; // check name/pw length //prepare hashed storage nsstring* hashedpassword = nil; unsigned char hashedpassworddata[cc_sha1_digest_length]; //hash 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 sent"]; return; } //check whether it's login or register nsstring* command = (sender.tag==1)?@"register":@"login"; nsmutabledictionary* params =[nsmutabledictionary dictionarywithobjectsandkeys: command, @"command", fldusername.text, @"username", hashedpassword, @"password", nil]; //make call 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 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
if figure out fix, i'll add here i'm not sure causing problem, appreciated.
you can use tap gesture hide keyboard
-(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]; }
Comments
Post a Comment