4

我正在开发一个共享扩展来简单地抓取一个链接,选择几个名字来分享它,然后分享。数据层尚未添加,只有 UI 用于在 tableview 中显示一些名称(使用自定义单元格),并且我正在从扩展上下文中提取共享 URL。VC中的所有代码如下。所有视图都在情节提要中设置。两个 UIButtons、两个 UILabels、一个 TableView 和一个 UIView 来容纳它,所以我可以轻松地绕过角落。

在此处输入图像描述

我遇到的问题是,_linkLabel我使用的显示 URL 在近 10 秒内没有视觉更新!What.In.The.World。我在这里做什么会导致这种情况?

我正在从回调中注销 URL,hasItemConformingToTypeIdentifier它会在扩展出现后立即发生,但不会更新标签??!!有帮助。请。

#import "ShareViewController.h"
#import "UserCell.h"

@interface ShareViewController ()

@end

@implementation ShareViewController

- (void)viewDidLoad{
    self.view.alpha = 0;
    _friends = [@[@"Ronnie",@"Bobby",@"Ricky",@"Mike"] mutableCopy];
    _containerView.layer.cornerRadius = 6.f;
    _selectedIndexPaths = [[NSMutableArray alloc] init];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIView animateWithDuration:0.5 animations:^{
        self.view.alpha = 1;
    }];
}

- (void)viewDidAppear:(BOOL)animated{
    //pull the URL out
    NSExtensionItem *item = self.extensionContext.inputItems[0];
    NSItemProvider *provider = item.attachments[0];
    if ([provider hasItemConformingToTypeIdentifier:@"public.url"]) {
        [provider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(id<NSSecureCoding> item, NSError *error) {
            NSURL *url = (NSURL*)item;
            _linkLabel.text = url.absoluteString;
            NSLog(@"Link: %@", url.absoluteString);
        }];
    }
    else{
        NSLog(@"No Link");
    }
}

#pragma mark - UITableView Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UserCell *cell = (UserCell*)[tableView cellForRowAtIndexPath:indexPath];
    if([_selectedIndexPaths containsObject:indexPath]){
        [_selectedIndexPaths removeObject:indexPath];
        cell.selected = NO;
    }
    else{
        cell.selected = YES;
        [_selectedIndexPaths addObject:indexPath];
    }
    NSLog(@"Share to %i friends", (int)[_selectedIndexPaths count]);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //Later, calc height based on text in comment
    return  44;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_friends count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"UserCell";
    UserCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        cell = [[UserCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.selected = ([_selectedIndexPaths containsObject:indexPath]) ? YES : NO;
    cell.nameLabel.text = [_friends objectAtIndex:indexPath.row];
    return cell;
}

- (IBAction)dismiss {
    [UIView animateWithDuration:0.34 animations:^{
        self.view.alpha = 0;
    } completion:^(BOOL finished) {
        [self.extensionContext completeRequestReturningItems:nil completionHandler:nil];
    }];
}

@end
4

1 回答 1

7

UI 元素更新延迟是尝试从主队列外部更新 UI 的典型标志。这就是这里发生的事情。你有这个:

[provider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(id<NSSecureCoding> item, NSError *error) {
    NSURL *url = (NSURL*)item;
    _linkLabel.text = url.absoluteString;
    NSLog(@"Link: %@", url.absoluteString);
}];

除了NSItemProvider不保证完成处理程序将在您开始的同一队列上调用。几乎可以保证您在这里排在不同的队列中,因此您会遇到这种奇怪的延迟。您需要分派回主队列以执行更新:

[provider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(id<NSSecureCoding> item, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSURL *url = (NSURL*)item;
        _linkLabel.text = url.absoluteString;
        NSLog(@"Link: %@", url.absoluteString);
    });
}];
于 2014-11-12T21:33:37.690 回答