简体   繁体   English

连接确实完成接收数据后,如何更改ios视图?

[英]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. 在此应用程序中,当用户按下按钮时,URL请求将被发送到服务器,并且一些相应的数据将被应用程序接收。

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. 然后在与segue相关的功能中,将对象发送到下一个视图。

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). 您不应该在connectionDidFinishLoading中显示视图控制器:因为似乎已经为控制器设置了segue -因为您尚未创建myTable(在.h文件中声明未创建,所以它在那里崩溃了)它)。 The segue will create the instance of your controller, so you should call performSegueWithIdentifier:sender: in connectionDidFinishLoading: segue将创建您的控制器的实例,因此您应该在connectionDidFinishLoading中调用performSegueWithIdentifier:sender ::

- (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];
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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