ios - XML 解析数据没有从 viewController 获取到 objective-c 中的 tableViewController?

标签 ios objective-c xml uiviewcontroller xml-parsing

我成功地从服务器获取了 xml 数据,但是我无法将该数据从 viewController 正确地发送到 tableViewController。我到底想做什么发送数据,这是我的代码......

在 View Controller 中...

- (IBAction)parserButtonClicked:(UIButton *)sender {

    [self.appTitleArray removeAllObjects];
    [self.releaseDateArray removeAllObjects];
    [self.summeryArray removeAllObjects];
    [self.priceArray removeAllObjects];
    [self.imageArray removeAllObjects];

    DisplayAppsTableViewController *datvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DATVC"];

    if ((self.countrySegmentControl.selectedSegmentIndex == 0 || self.countrySegmentControl.selectedSegmentIndex == 1 || self.countrySegmentControl.selectedSegmentIndex == 2) && (self.categorySegmentControl.selectedSegmentIndex == 0 || self.categorySegmentControl.selectedSegmentIndex == 1) && (self.countSegmentControl.selectedSegmentIndex == 0 || self.countSegmentControl.selectedSegmentIndex == 1 || self.countSegmentControl.selectedSegmentIndex == 2 || self.countSegmentControl.selectedSegmentIndex == 3)) {

        [self stringManipulation];
//This is the link rom stringManipulation method
// https://itunes.apple.com/in/rss/topfreeapplications/limit=10/xml
// https://itunes.apple.com/in/rss/toppaidapplications/limit=10/xml

self.URLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.XMLurl]];

self.URLSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

self.dataTask = [self.URLSession dataTaskWithRequest:self.URLRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        self.XMLParser = [[NSXMLParser alloc]initWithData:data];

        self.XMLParser.delegate = self;

        BOOL success = [self.XMLParser parse];

            if (success) {
                NSLog(@"Parse success");
            } else {
                NSLog(@"Parse not success");
            }

     }];
        [self.dataTask resume];

}

    datvc.appTitleArray1 = self.appTitleArray;
    datvc.releaseDateArray1 = self.releaseDateArray;
    datvc.priceArray1 = self.priceArray;
    datvc.summeryArray1 = self.summeryArray;
    datvc.imageArray1 = self.imageArray;
    NSLog(@"Here it's NOT printing data successfully : %@", datvc.appTitleArray1);

    [self.navigationController pushViewController:datvc animated:YES];

}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict {

    self.element = elementName;

   if([elementName isEqualToString:@"im:releaseDate"]) {

        NSString *date=[attributeDict objectForKey:@"label"];
    [self.releaseDateArray addObject:date];
   }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                           [NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if (trimmedString.length > 0) {

        if ([self.element isEqualToString:@"im:name"]) {
        [self.appTitleArray addObject:string];
        }
        if ([self.element isEqualToString:@"summary"]) {
        [self.summeryArray addObject:string];
        }
        if ([self.element isEqualToString:@"im:price"]) {
        [self.priceArray addObject:string];
        }
        if ([self.element isEqualToString:@"im:image"]) {
        [self.imageArray addObject:string];
        }

    }

    NSLog(@"Here it's parsing data successfully : %lu",     [self.appTitleArray count]);
}

在tableViewController中

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog(@"Not getting data from viewController : %lu",  [self.releaseDateArray1 count]);

    return [self.releaseDateArray1 count];
}

最佳答案

不喜欢

DisplayAppsTableViewController *datvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DATVC"];

    if ((self.countrySegmentControl.selectedSegmentIndex == 0 || self.countrySegmentControl.selectedSegmentIndex == 1 || self.countrySegmentControl.selectedSegmentIndex == 2) && (self.categorySegmentControl.selectedSegmentIndex == 0 || self.categorySegmentControl.selectedSegmentIndex == 1) && (self.countSegmentControl.selectedSegmentIndex == 0 || self.countSegmentControl.selectedSegmentIndex == 1 || self.countSegmentControl.selectedSegmentIndex == 2 || self.countSegmentControl.selectedSegmentIndex == 3)) {

        [self stringManipulation];
//This is the link rom stringManipulation method
// https://itunes.apple.com/in/rss/topfreeapplications/limit=10/xml
// https://itunes.apple.com/in/rss/toppaidapplications/limit=10/xml

self.URLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.XMLurl]];

self.URLSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

self.dataTask = [self.URLSession dataTaskWithRequest:self.URLRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        self.XMLParser = [[NSXMLParser alloc]initWithData:data];

        self.XMLParser.delegate = self;

        BOOL success = [self.XMLParser parse];

            if (success) {
                NSLog(@"Parse success");
            } else {
                NSLog(@"Parse not success");
            }

     }];
        [self.dataTask resume];

}

    datvc.appTitleArray1 = self.appTitleArray;
    datvc.releaseDateArray1 = self.releaseDateArray;
    datvc.priceArray1 = self.priceArray;
    datvc.summeryArray1 = self.summeryArray;
    datvc.imageArray1 = self.imageArray;
    NSLog(@"Here it's NOT printing data successfully : %@", datvc.appTitleArray1);

    [self.navigationController pushViewController:datvc animated:YES];

喜欢

reason : you are called the additional thread , in it will take some additional time for execution , after the successful execution you need to navigate.

    if ((self.countrySegmentControl.selectedSegmentIndex == 0 || self.countrySegmentControl.selectedSegmentIndex == 1 || self.countrySegmentControl.selectedSegmentIndex == 2) && (self.categorySegmentControl.selectedSegmentIndex == 0 || self.categorySegmentControl.selectedSegmentIndex == 1) && (self.countSegmentControl.selectedSegmentIndex == 0 || self.countSegmentControl.selectedSegmentIndex == 1 || self.countSegmentControl.selectedSegmentIndex == 2 || self.countSegmentControl.selectedSegmentIndex == 3)) {

        [self stringManipulation];
//This is the link rom stringManipulation method
// https://itunes.apple.com/in/rss/topfreeapplications/limit=10/xml
// https://itunes.apple.com/in/rss/toppaidapplications/limit=10/xml

self.URLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.XMLurl]];

self.URLSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

self.dataTask = [self.URLSession dataTaskWithRequest:self.URLRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        self.XMLParser = [[NSXMLParser alloc]initWithData:data];

        self.XMLParser.delegate = self;

        BOOL success = [self.XMLParser parse];

            if (success) {
                NSLog(@"Parse success");

               dispatch_sync(dispatch_get_main_queue(), ^{
// Update the UI on the main thread.





                   DisplayAppsTableViewController *datvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DATVC"];
                  datvc.appTitleArray1 = self.appTitleArray;
    datvc.releaseDateArray1 = self.releaseDateArray;
    datvc.priceArray1 = self.priceArray;
    datvc.summeryArray1 = self.summeryArray;
    datvc.imageArray1 = self.imageArray;
    NSLog(@"Here it's NOT printing data successfully : %@", datvc.appTitleArray1);

    [self.navigationController pushViewController:datvc animated:YES];
     });
            } else {
                NSLog(@"Parse not success");
            }

     }];
        [self.dataTask resume];

}

关于ios - XML 解析数据没有从 viewController 获取到 objective-c 中的 tableViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42647830/

相关文章:

Android:来自 xml 布局的自定义 View

ios - 在选择另一个UITEXTVIEW时,如何阻止UitextView辞职急救/关闭键盘?

ios - 从 Cordova iOS 中的委托(delegate)返回数据

html - 溢出-x : auto on iPhone is not scrolling

ios - 在 setting.app 中添加 Respring 按钮

ios - 查询从 Parse DB 中检索当前登录用户的 BOOL,这将确定要转到的 segue

sql - SQL Server 复制期间的空 XML 列

ios - 当两个应用监控同一个 iBeacon 区域时会发生什么?

ios - 使用 Storyboard在 swift 中创建自定义 TableViewCell

c# - FileSystemWatcher 丢失文件