Friday, 15 July 2011

ios - Code keeps crashing with passing data -



ios - Code keeps crashing with passing data -

i'm building first ios app login system. i've built simple things before, on whole new level. i'm able login , info server. app performs model segue mainview maincontroller attached it.

when log in, app gets token , success server. token needed farther requests server. however, can't seem figure out how pass token maincontroller. doesn't matter try, app crashes unknown error.

right - code below - throws:

2014-10-11 14:30:14.077 loginscreen[29076:4688502] -[uinavigationcontroller setxauthtoken:]: unrecognized selector sent instance 0x7bfd1140 2014-10-11 14:30:14.104 loginscreen[29076:4688502] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[uinavigationcontroller setxauthtoken:]: unrecognized selector sent instance 0x7bfd1140'

viewcontroller.h

#import <uikit/uikit.h> #import "maincontroller.h" @interface viewcontroller : uiviewcontroller <uitextfielddelegate> @property (weak, nonatomic) iboutlet uitextfield *txtusername; @property (weak, nonatomic) iboutlet uitextfield *txtpassword; - (ibaction)sigininclicked:(id)sender; - (ibaction)backgroundtap:(id)sender; @end

viewcontroller.m

#import "viewcontroller.h" @interface viewcontroller () @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (ibaction)sigininclicked:(id)sender { nsinteger success = 0; @try { if([[self.txtusername text] isequaltostring:@""] || [[self.txtpassword text] isequaltostring:@""] ) { [self alertstatus:@"please come in email , password" :@"sign in failed!" :0]; } else { nsstring *post =[[nsstring alloc] initwithformat:@"username=%@&password=%@",[self.txtusername text],[self.txtpassword text]]; nslog(@"postdata: %@",post); nsurl *url=[nsurl urlwithstring:@"http://www.mywebsite.com/auth"]; nsdata *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes]; nsstring *postlength = [nsstring stringwithformat:@"%lu", (unsigned long)[postdata length]]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; [request seturl:url]; [request sethttpmethod:@"post"]; [request setvalue:postlength forhttpheaderfield:@"content-length"]; [request setvalue:@"application/json" forhttpheaderfield:@"accept"]; [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"]; [request sethttpbody:postdata]; //[nsurlrequest setallowsanyhttpscertificate:yes forhost:[url host]]; nserror *error = [[nserror alloc] init]; nshttpurlresponse *response = nil; nsdata *urldata=[nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; nslog(@"response code: %ld", (long)[response statuscode]); if ([response statuscode] >= 200 && [response statuscode] < 300) { nsstring *responsedata = [[nsstring alloc]initwithdata:urldata encoding:nsutf8stringencoding]; nslog(@"response ==> %@", responsedata); nserror *error = nil; nsdictionary *jsondata = [nsjsonserialization jsonobjectwithdata:urldata options:nsjsonreadingmutablecontainers error:&error]; success = [jsondata[@"success"] integervalue]; nslog(@"success: %ld",(long)success); if(success == 1) { nslog(@"login success"); } else { nsstring *error_msg = (nsstring *) jsondata[@"error_message"]; [self alertstatus:error_msg :@"sign in failed!" :0]; } } else { //if (error) nslog(@"error: %@", error); [self alertstatus:@"connection failed" :@"sign in failed!" :0]; } } } @catch (nsexception * e) { nslog(@"exception: %@", e); [self alertstatus:@"sign in failed." :@"error!" :0]; } if (success) { [self performseguewithidentifier:@"login_success" sender:self]; } } - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([segue.identifier isequaltostring:@"login_success"]) { maincontroller *controller = (maincontroller *)segue.destinationviewcontroller; controller.xauthtoken = @"test string"; } } - (void) alertstatus:(nsstring *)msg :(nsstring *)title :(int) tag { uialertview *alertview = [[uialertview alloc] initwithtitle:title message:msg delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil]; alertview.tag = tag; [alertview show]; } - (ibaction)backgroundtap:(id)sender { [self.view endediting:yes]; } -(bool) textfieldshouldreturn:(uitextfield *)textfield { [textfield resignfirstresponder]; homecoming yes; } @end

maincontroller.h

#import <foundation/foundation.h> @interface maincontroller : uiviewcontroller @property (nonatomic,strong) nsstring *xauthtoken; @end

maincontroller.m

#import "maincontroller.h" @interface maincontroller () @end @implementation maincontroller - (void)viewdidload { nslog(@"%@",_xauthtoken); } @end

the stack trace tells problem - trying set xauthtoken property on uinavigationcontroller - uinavigationcontroller doesn't have property.

your maincontroller instance embedded in uinavigationcontroller, destinationviewcontroller in prepareforsegue.

you need access view controller stack -

- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([segue.identifier isequaltostring:@"login_success"]) { uinavigationcontroller *navcontroller = (uinavigationcontroller *)segue.destinationviewcontroller; maincontroller *controller = (maincontroller *)navcontroller.topviewcontroller controller.xauthtoken = @"test string"; } }

ios objective-c

No comments:

Post a Comment