我有一个视图控制器,它在viewDidLoad方法中注册预览:
- (void) viewDidLoad
{
[super viewDidLoad];
if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] &&
(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable))
{
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}
}
但有时无法识别强制触摸,就好像视图控制器没有注册预览一样。
从状态恢复启动应用程序时出现问题。修复:在视图中放置预览注册代码WillAppear:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] &&
(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable))
{
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}
}
当从状态恢复调用viewDidLoad时,注册预览的时机正合适。