Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone Objective-C: combining methods together
    text
    copied!<p>I'm a complete noob at objective-C, so this might be a very silly question to a lot of you.</p> <p>Currently, I have a view with a 'SignIn' button, which when clicked, activates a sigupUp IBAction that I have defined below. Basically, this method needs to make a JSON call and get back some data about the user.</p> <p>So, right now, my code looks something like this:</p> <pre><code>-(IBAction)signIn:(id)sender{ //run registration API call //establish connection NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.apicallhere.com/api/auth"]]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"]; [request setHTTPBody:postData]; [[NSURLConnection alloc]initWithRequest:request delegate:self]; responseData = [[NSMutableData data] retain]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{[responseData setLength:0];} - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{[responseData appendData:data];} - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{/*NSLog(@"%@",error);*/} -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSString *response=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; ....code carries on from here. } </code></pre> <p>As you can see, the problem with my code is that even though it is initiated via the 'SignUp' method, it finishes with the 'connectionDidFinishLoading' method. I want to have all this code in a single method. Not 2 separate ones, as I want to be able to return a boolean verifying if the connection was successful or not.</p> <p>If someone could please tell me how to code up this procedure into a single method, I would really appreciate it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload