【问题标题】:How can I change ios view when connection did finish receiving the data?连接完成接收数据后,如何更改 ios 视图?
【发布时间】:2013-09-05 13:34:24
【问题描述】:

在此应用程序中,当用户按下按钮时,将向服务器发送 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 相关的函数中,我会将对象发送到下一个视图。

【问题讨论】:

  • 不可能说没有更多代码(建议粘贴整个类)和你的崩溃细节。
  • 发布错误信息。
  • 我从 .m 文件中添加了所有重要的部分。
  • responseData 属性已被注释掉。你真的有这个@property吗?
  • 您遇到了什么错误?

标签: ios ios6 uiviewcontroller nsurlconnection


【解决方案1】:

您不应该在 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];
 }

【讨论】:

  • 很抱歉,因为我的声誉低,我不能投票给你 :))
    顺便说一下,我没有在情节提要中设置任何转场......因为我无法定义按钮等待从服务器加载所有数据然后执行segue...所以我只是使用presentViewController方法...我修复了声明myTableView tnx的问题。
  • @user2725916,我假设你已经设置了一个 segue,因为你实现了 prepareForSegue。如果您没有设置 segue,为什么要这样做?顺便说一句,如果您从代码中调用 performSegue,则不需要情节提要中的按钮——您可以直接从控制器到另一个控制器进行 segue,并给它一个标识符。然后你可以像我在回答中显示的那样做。
  • 实际上,正如我所提到的,我不想在数据接收未完成之前加载页面。我尝试从情节提要中进行 segue,但是一旦单击按钮,它就会更改页面,但我需要做其他事情。所以我决定从故事板上删除segue。并且仍然不知道如何更改视图并仅通过编码传递参数。
  • “仍然不知道如何更改视图并仅通过编码传递参数”-您在第一条评论中说您解决了问题。这不会改变视图并传递数据吗?你明白我最后的评论了吗?您可以像我在我的答案中显示的那样使用 segue 执行此操作,只要该 segue 是由控制器(而不是按钮)制作的 - 在您在代码中调用 performSegueWithIdentifier 之前不会触发 segue,这是在 connectionDidFinishLoading 中完成的.
  • 非常感谢您的解释,这真的很有帮助...现在我也使用您的建议方法通过了它...效果很好...谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多