簡體   English   中英

連接確實完成接收數據后,如何更改ios視圖?

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

在此應用程序中,當用戶按下按鈕時,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相關的功能中,將對象發送到下一個視圖。

您不應該在connectionDidFinishLoading中顯示視圖控制器:因為似乎已經為控制器設置了segue -因為您尚未創建myTable(在.h文件中聲明未創建,所以它在那里崩潰了)它)。 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