我正在制作共享扩展程序,只需抓取一个链接,选择几个名称即可分享,然后分享。尚未添加数据层,只有用于在tableview中显示某些名称的UI(使用自定义单元格),并且我从扩展上下文中提取共享URL。 VC中的所有代码都在下面。所有视图都在故事板中设置。两个UIButtons,两个UILabels,一个TableView和一个UIView来支撑它,所以我可以很容易地绕过角落。
我遇到的问题是我使用显示的_linkLabel
URL在近10秒内无法直观更新! What.In.The.World。我在这做什么导致了这个?
我正在退出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
答案 0 :(得分: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);
});
}];