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

时间:2013-09-05 13:34:25

标签: ios ios6 uiviewcontroller nsurlconnection

在此应用程序中,当用户按下按钮时,url请求将被发送到服务器,并且应用程序将接收一些相应的数据。

我的问题是我想完全从服务器接收数据,当连接完成接收数据时,更改视图并将接收的数据传递给另一个视图。

转换数据后,我收到错误和应用程序崩溃。

我试过这种方法,但不知道我该怎么做。
解决问题并编辑代码

 #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

然后在与segue相关的函数中,我将对象发送到下一个视图。

1 个答案:

答案 0 :(得分:0)

你不应该在connectionDidFinishLoading中呈现视图控制器:因为看起来你已经为控制器设置了一个segue - 它正在崩溃,因为你还没有创建myTable(在.h文件中声明它没有不创造它。 segue将创建控制器的实例,因此您应该调用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];
 }