1

我目前正在我的应用程序中实现 Peek and Pop。我有一个UITableView包含一些数据。我编写了这个函数来处理 Peek:

- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    // check if we're not already displaying a preview controller
    if ([self.presentedViewController isKindOfClass:[DetailViewController class]]) {
        return nil;
    }

    NSLog(@"Test");

    CGPoint cellPostion = [self.tableView convertPoint:location fromView:self.view];
    NSIndexPath *path = [self.tableView indexPathForRowAtPoint:cellPostion];

    if (path) {
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
        NSDictionary *data = [self.restaurants objectAtIndex:path.row];
        // shallow press: return the preview controller here (peek)
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        DetailViewController *previewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
        previewController.restaurant = data;
        [previewingContext setSourceRect:cell.frame];
        return previewController;
    }
    return nil;
}

是这样构造的UITableView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.title = @"Overview";

    self.restaurants = [sRestaurants fetchAll];
    NSLog(@"%@", self.subscriptions);

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

但是,现在,它甚至没有登录Test到控制台。我究竟做错了什么?

4

1 回答 1

0

您是否将此委托添加到您的 ViewController

UIViewControllerPreviewingDelegate

您还需要注册Force Touch

if (IS_OS_9_OR_LATER) {
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        [self registerForPreviewingWithDelegate:self sourceView:self.view];
    }
}
于 2016-01-14T11:28:44.847 回答