Background Fetch iOS UIBackgroundFetchResult 无法识别的选择器

Background Fetch iOS UIBackgroundFetchResult unrecognize selector

我正在关注 AppCoda:Working with Background Fetch Programming,但遇到了一些错误。我已经创建了基础应用程序,可以显示 table 来自其来源的数据,而且它 UIRefreshControl 运行良好。

然后我开始创建它的后台抓取进程。我已经启用了它的后台功能,在 appDelegate 上设置了最小间隔时间,并实现了 application:performFetchWithCompletionHandler: 方法。

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    MyTableViewController *viewController = (MyTableViewController *)self.window.rootViewController;
    [viewController fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
        completionHandler(result);
    }];
}

我构建了这个项目,并且仍然运行良好。然后我想通过复制现有项目方案并启用选项 launch due to a background fetch event 来测试其后台进程。我按 运行,由于无法识别选择器,它给我错误。

[UINavigationController fetchNewDataWithCompletionHandler:]: unrecognized selector sent to instance 0x7fb99280b800

这是我的 fetchNewDataWithCompletionHandler:

- (void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    XMLParser *xmlParser = [[XMLParser alloc] initWithXMLURLString:NewsFeed];
    [xmlParser startParsingWithCompletionHandler:^(BOOL success, NSArray *dataArray, NSError *error) {
        if (success) {
            NSDictionary *latestDict = [dataArray objectAtIndex:0];
            NSString *latestTitle = [latestDict objectForKey:@"title"];

            NSDictionary *existingDict = [self.arrNewsData objectAtIndex:0];
            NSString *existingTilte = [existingDict objectForKey:@"title"];

            if ([latestTitle isEqualToString:existingTilte]) {
                completionHandler(UIBackgroundFetchResultNoData);
                NSLog(@"No new data found");
            }
            else {
                [self performNewFetchedDataActionsWithDataArray:dataArray];
                completionHandler(UIBackgroundFetchResultNewData);
                NSLog(@"New data was fetched");
            }
        }
        else {
            completionHandler(UIBackgroundFetchResultFailed);
            NSLog(@"Failed to fetch");
        }
    }];
}

我不知道这是怎么回事,我的 fetchNewDataWithCompletionHandler 怎么了?。任何帮助,将不胜感激。谢谢。

您需要将 viewController 设置为 rootViewController

MyTableViewController *viewController = [[MyTableViewController alloc] init];
self.window.rootViewController = viewController;

然后配置您的tableView:cellForRowAtIndexPath:以支持出队单元标识符

static NSString *cellIdentifier = @"cellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}

如果可行,请告诉我。