[英]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.