带有故事板 segues 的上下文菜单预览提供程序
Context menu preview provider with storyboard segues
在我的故事板中,我有一个 table 带有静态单元格的视图控制器,每个单元格都执行一个 segue 来呈现视图控制器。
到目前为止,可以在 Storyboard 中启用 Peek 和 Pop 支持,并且它将为此实现所有遗留 API。
上下文菜单目前似乎没有这样的选项。
我的问题是,是否有可能以某种方式按需从 segue 获取控制器,以便我可以通过 UIContextMenuContentPreviewProvider
return 它?欢迎任何其他建议(除了更改故事板中的静态单元格)!
用私有API解决了这个问题,但是这个不应该被使用! 仅用于教育目的!
我给了每个单元格和 segue 相同的标识符。然后使用私有 APIs,我为 segue 和 return 实例化目标控制器作为预览。
让我们声明一些私有 APIs:
@interface UIViewController ()
- (id)_segueTemplateWithIdentifier:(id)arg1;
@end
@interface NSObject ()
- (id)instantiateOrFindDestinationViewControllerWithSender:(id)arg1;
@end
然后在实现中将其捆绑在一起:
@implementation DemoGalleryController
- (nullable UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point API_AVAILABLE(ios(13.0))
{
return [UIContextMenuConfiguration configurationWithIdentifier:@"Preview" previewProvider:^ UIViewController* {
NSString* cellIdentifier = [tableView cellForRowAtIndexPath:indexPath].reuseIdentifier;
id segueTemplate = [self _segueTemplateWithIdentifier:cellIdentifier];
return [segueTemplate instantiateOrFindDestinationViewControllerWithSender:self];;
} actionProvider:nil];
}
- (void)tableView:(UITableView *)tableView willPerformPreviewActionForMenuWithConfiguration:(UIContextMenuConfiguration *)configuration animator:(id<UIContextMenuInteractionCommitAnimating>)animator API_AVAILABLE(ios(13.0))
{
UIViewController* vc = animator.previewViewController;
[animator addCompletion:^{
[self presentViewController:vc animated:YES completion:nil];
}];
}
@end
在我的故事板中,我有一个 table 带有静态单元格的视图控制器,每个单元格都执行一个 segue 来呈现视图控制器。
到目前为止,可以在 Storyboard 中启用 Peek 和 Pop 支持,并且它将为此实现所有遗留 API。
上下文菜单目前似乎没有这样的选项。
我的问题是,是否有可能以某种方式按需从 segue 获取控制器,以便我可以通过 UIContextMenuContentPreviewProvider
return 它?欢迎任何其他建议(除了更改故事板中的静态单元格)!
用私有API解决了这个问题,但是这个不应该被使用! 仅用于教育目的!
我给了每个单元格和 segue 相同的标识符。然后使用私有 APIs,我为 segue 和 return 实例化目标控制器作为预览。
让我们声明一些私有 APIs:
@interface UIViewController ()
- (id)_segueTemplateWithIdentifier:(id)arg1;
@end
@interface NSObject ()
- (id)instantiateOrFindDestinationViewControllerWithSender:(id)arg1;
@end
然后在实现中将其捆绑在一起:
@implementation DemoGalleryController
- (nullable UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point API_AVAILABLE(ios(13.0))
{
return [UIContextMenuConfiguration configurationWithIdentifier:@"Preview" previewProvider:^ UIViewController* {
NSString* cellIdentifier = [tableView cellForRowAtIndexPath:indexPath].reuseIdentifier;
id segueTemplate = [self _segueTemplateWithIdentifier:cellIdentifier];
return [segueTemplate instantiateOrFindDestinationViewControllerWithSender:self];;
} actionProvider:nil];
}
- (void)tableView:(UITableView *)tableView willPerformPreviewActionForMenuWithConfiguration:(UIContextMenuConfiguration *)configuration animator:(id<UIContextMenuInteractionCommitAnimating>)animator API_AVAILABLE(ios(13.0))
{
UIViewController* vc = animator.previewViewController;
[animator addCompletion:^{
[self presentViewController:vc animated:YES completion:nil];
}];
}
@end