简体   繁体   中英

How can I change ios view when connection did finish receiving the data?

In this application when user presses a button a url request will be sent to a server and some corresponding data will be received by the application.

My problem is I want to receive the data completely from the server and when the connection did finished receiving the data, change the view and pass the received data to the other view.

After the conversion of data I receive error and app crashes.

I tried this approach but don't know what should I do exactly.
Problem Solved and Code is Edited

 #import "ViewController.h"
@interface ViewController ()
//@property (nonatomic, strong) NSMutableData *responseData;

@end

@implementation ViewController
@synthesize responseData;
@synthesize myTableView;
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"viewdidload");
}
- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

   NSLog(@"Response received From the server.");
  [self.responseData setLength:0];
}
  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

  NSLog(@"appending data to the response object.");
  [self.responseData appendData:data];

}
 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

   NSLog(@"didFailWithError");
   NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
     NSLog(@"Loading data succeeded! Received %d bytes of data", 
       [self.responseData length]);
     //call the converter to convert the received 
     //Data to Json Packet and save to variable
     [self convertDataToJson];
      [self changePage];

 }
 -(void)changePage {
      resultsTableViewController *myTableView = [[resultsTableViewController  alloc]init];
       myTableView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        myTableView=[myTableView init];
        [self presentViewController:myTableView animated:YES completion:NULL];

 }


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
   resultsTableViewController *mytable = [segue destinationViewController];
   NSMutableArray * response = self.responseArray;
   mytable.responseArray = response;

   }

- (IBAction)get:(id)sender {


  //making an instance of data query object and passing the word and dictionary selection to it.
   if ( ![[searchField text]isEqualToString:@""] )
   {
       word = @”test”
       [self makeRequestForWord:word withDictionaryName:@""];
       // NSData *query = [word dataUsingEncoding:NSUTF8StringEncoding];
  }
 }



-(void)makeRequestForWord:(NSString*)word withDictionaryName:(NSString*)dicSelect;
{

    //creating URL Request Object using given URL object.
    //It requests data using nsConnection

}


- (void)convertDataToJson
{
// some conversion and save into responseDATA
}

- (void)viewDidUnload {
   [super viewDidUnload];
}
@end

And then in function related to segue I'll send the object to the next view.

You shouldn't be presenting the view controller in connectionDidFinishLoading: since it appears that you have a segue set up for the controller -- it's crashing there because you haven't created myTable yet (declaring it in the .h file doesn't create it). The segue will create the instance of your controller, so you should call performSegueWithIdentifier:sender: in connectionDidFinishLoading:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
     NSLog(@"Loading data succeeded! Received %d bytes of data", 
       [self.responseData length]);
     //call the converter to convert the received 
     //Data to Json Packet and save to variable
     [self convertDataToJson];

     [self performSegueWithIdentifier:@"MyIdentifier" sender:self];
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM