速览UIView显示SFSafariViewController

时间:2019-07-10 08:49:50

标签: ios swift xcode 3dtouch

我有一个UITableView,并且在每个单元格中都有一个UIView作为子视图。我现在拥有的是UITapGestureRecognizer,只要我单击其中的In-App-Safari之一,即可打开UIViews。但是,我正在努力实现Apples sample code的peek and pop功能以实现强制触摸。我的代码如下:

 @IBOutlet var mainTableView: UITableView!

viewDidLoad()中:

 registerForPreviewing(with: self as! UIViewControllerPreviewingDelegate, sourceView: mainTableView)

viewDidLoad()之后:

 func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
            // First, get the index path and view for the previewed cell.
        guard let indexPath = tableView.indexPathForRow(at: location),
              let cell = tableView.cellForRow(at: indexPath)
              else { return nil }

 //Enable blurring of other UI elements, and a zoom in animation while peeking.

 previewingContext.sourceRect = cell.frame

 //Create and configure an instance of the color item view controller to show for the peek.

 guard let url = someURL else { return nil }
            let vc = SFSafariViewController(url: url)
            vc.delegate = self as SFSafariViewControllerDelegate

            return vc
        }

        func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) 
{

//Push the configured view controller onto the navigation stack.

 navigationController?.pushViewController(viewControllerToCommit, animated: true)
}

Xcode返回错误,例如

  

收到的内存压力事件4 vm pressure 1

  

由于未捕获的异常'NSInvalidArgumentException'而终止了应用程序,原因:'-[ViewController PreviewingContext:viewControllerForLocation:]:无法识别的选择器发送到实例

我还没有找到一种解决方法,可以在按UIView时窥视和弹出Web内容。

1 个答案:

答案 0 :(得分:0)

好的,所以我发现我只是忘记将UIViewControllerPreviewingDelegate添加到我的班级:

class MyViewController: UITableViewController, UIViewControllerPreviewingDelegate, SFSafariViewControllerDelegate

窥视按规定的方法按预期工作,但是要使流行音乐起作用,我必须调整最后一个功能:

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
    show(viewControllerToCommit, sender: self)
}