objective c - how to access value form completionHandler and pass it to another view IOS -


i'm trying return string method have 2 class

first 1 ui , have 2 input text user , pass , have submit button , 1 doing following method .

i'm trying return string other class class , show string in alert .

#import "loginpage.h"  @implementation loginpage  -(nsstring *)responsdata:(nsstring *)loginurl input1:(nsstring *)username input2:(nsstring *)password {      nsstring *urlasstring = loginurl;     nsstring*test;       inusername = username;     inpassword = password;      nsurl *url = [nsurl urlwithstring:urlasstring];     nsmutableurlrequest *urlrequest = [nsmutableurlrequest requestwithurl:url];     [urlrequest settimeoutinterval:30.0f];     [urlrequest sethttpmethod:@"post"];      // setting username , password     nsstring *body = [nsstring stringwithformat:@"sended=yes&username=%@&password=%@",username,password];      [urlrequest sethttpbody:[body datausingencoding:nsutf8stringencoding]];     nsoperationqueue *queue = [[nsoperationqueue alloc] init];     [nsurlconnection      sendasynchronousrequest:urlrequest      queue:queue      completionhandler:^(nsurlresponse *response,                          nsdata *data,                          nserror *error)      {          if ([data length] >0 && error == nil){ nsstring *html =              [[nsstring alloc] initwithdata:data                                    encoding:nsutf8stringencoding];             // nslog(@"%@", html);              self.lastvalue = [nsstring stringwithformat:@"%@",html];          }          else if ([data length] == 0 && error == nil){              //nslog(@"nothing downloaded.");               self.lastvalue = [nsstring stringwithformat:@"no thing downloaded"];          }          else if (error != nil){            //  nslog(@"error happened = %@", error);               self.lastvalue = [nsstring stringwithformat:@"%@",error];          } }];       nslog(@"%@",self.lastvalue);     return  self.lastvalue; }  // additional setup after loading view, typically nib.    @end 

i want use function in view ( include header of file ) can't , can 1 solve >

another view

- (ibaction)submit:(id)sender {      loginpage * login = [[loginpage alloc]init];    nsstring * datare;     datare =  [login responsdata:@"http://fahads-macbook-pro.local/ios/post.php" input1:@"admin" input2:@"1234"];     nslog(@"%@",login.lastvalue);     if (datare != nil) {         uialertview * alert =[[uialertview alloc]         initwithtitle:@"hello fahad"           message:[nsstring stringwithformat:@"%@",datare] delegate:self cancelbuttontitle:@"okay ! " otherbuttontitles:nil, nil];         [alert show];     }   } 

thank again

when call function on other view, send asynch request web. when do:

return self.lastvalue; 

lastvalue is still empty or previous value because competionhandler need still called. code of completionhandler, peace of code passed function, called @ right moment. function arrive end return.

when instead completion handler block called (because request has produced response), assign value:

self.lastvalue = [nsstring stringwithformat:@"%@",html]; 

now lastvalue right.

so function shouldn't return an nsstring, should return void. pass string other controller, you should use the delegation pattern.

this example

secondviewcontroller.h

@protocol secondviewcontrollerdelegate <nsobject>  - (void)lastvaluedidupdate:(nsstring *)lastvalue;  @end  @interface secondviewcontroller : uiviewcontroller  @property (weak, nonatomic) id<secondviewcontrollerdelegate>delegate;  @end 

secondviewcontroller.m

#import "secondviewcontroller.h" @implementation secondviewcontroller  -(nsstring *)responsdata:(nsstring *)loginurl input1:(nsstring *)username input2:(nsstring *)password {      nsstring *urlasstring = loginurl;     nsstring*test;       inusername = username;     inpassword = password;      nsurl *url = [nsurl urlwithstring:urlasstring];     nsmutableurlrequest *urlrequest = [nsmutableurlrequest requestwithurl:url];     [urlrequest settimeoutinterval:30.0f];     [urlrequest sethttpmethod:@"post"];      // setting username , password     nsstring *body = [nsstring stringwithformat:@"sended=yes&username=%@&password=%@",username,password];      [urlrequest sethttpbody:[body datausingencoding:nsutf8stringencoding]];     nsoperationqueue *queue = [[nsoperationqueue alloc] init];      __weak typeof (self) weakself = self;      [nsurlconnection      sendasynchronousrequest:urlrequest      queue:queue      completionhandler:^(nsurlresponse *response,                          nsdata *data,                          nserror *error)      {          if ([data length] >0 && error == nil){ nsstring *html =              [[nsstring alloc] initwithdata:data                                    encoding:nsutf8stringencoding];              // nslog(@"%@", html);              weakself.lastvalue = [nsstring stringwithformat:@"%@",html];          }          else if ([data length] == 0 && error == nil){              //nslog(@"nothing downloaded.");              weakself.lastvalue = [nsstring stringwithformat:@"no thing downloaded"];          }          else if (error != nil){              //  nslog(@"error happened = %@", error);              weakself.lastvalue = [nsstring stringwithformat:@"%@",error];          }           [weakself.delegate lastvaluedidupdate:weakself.lastvalue];       }]; }   @end 

firstviewcontroller.h

#import "secondviewcontroller.h"  @interface firstviewcontroller : uiviewcontroller <secondviewcontrollerdelegate>  @property (strong, nonatomic) secondviewcontroller *secondviewcontroller;  @end 

firstviewcontroller.m

#import "firstviewcontroller.h"  @implementation firstviewcontroller  - (void)viewdidload {     //your code      [_secondviewcontroller setdelegate:self];      //your code }  //your code  @end 

note use weak reference self because otherwise can create retain cycle.


Comments

Popular posts from this blog

android - Automated my builds -

how to proxy from https to http with lighttpd -

python - Flask migration error -