简体   繁体   English

快速进行3D预览和POP

[英]3D Peek and POP in swift

I'm trying to implement 3D Touch Peek and Pop in my swift code. 我正在尝试在我的快速代码中实现3D Touch Peek和Pop。 When the user presses deeper on the peek view, array of preview actions will appear (Share, Update, Delete). 当用户在速览视图上按得更深时,将显示一系列预览操作(共享,更新,删除)。

What I need is when user select Update action will move to UpdateView controller, but it keeps on crashing. 我需要的是,当用户选择“更新”操作将移至“ UpdateView”控制器时,它仍然崩溃。

Here is my code: 这是我的代码:

HomePeakViewController.swift HomePeakViewController.swift

let item3 = UIPreviewAction(title: "Update", style: .Default) { (action:UIPreviewAction, vc:UIViewController) -> Void in
        print("Update")

        let nb:BookAppointmentViewController = BookAppointmentViewController(nibName: "BookAppointmentViewController", bundle: nil)

        let root = UIApplication.sharedApplication().keyWindow?.rootViewController
        root?.presentViewController(nb, animated: true, completion: nil)

POP method in HomeViewController.swift HomeViewController.swift中的POP方法

func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {

        let Homepeak = HomePeakViewController()
        showViewController(Homepeak, sender: self)

    }

I tried this code as well to move to Update screen, but it gives me (fatal error: unexpectedly found nil while unwrapping an Optional value). 我也尝试了此代码以移动到“更新”屏幕,但是它给了我(致命错误:在展开Optional值时意外发现nil)。

var top = UIApplication.sharedApplication().keyWindow?.rootViewController

            let test = AppointmentDetailsViewController()
            top!.presentViewController(test, animated: true, completion: {})

Maybe you need to deal with delegations: 也许您需要与代表团打交道:

For example: 例如:

extension MainViewController: UIViewControllerPreviewingDelegate {

    func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        if #available(iOS 9.0, *) {
            previewingContext.sourceRect = myButton!.bounds //optional
        }

        let homePeakViewController = UIStoryboard.homePeakViewController()
        homePeakViewController.delegate = self

        return homePeakViewController
    }


    func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
        let balanceViewController = viewControllerToCommit as! HomePeakViewController
        navigationController?.pushViewController(balanceViewController, animated: true)
    }

}

extension MainViewController: HomePeakViewControllerDelegate {

    func homePeakViewControllerUpadateActionTapped() {
      let bookAppointmentViewController = let nb:BookAppointmentViewController = BookAppointmentViewController(nibName: "BookAppointmentViewController", bundle: nil)
      navigationController?.pushViewController(bookAppointmentViewController, animated: true) //present as you want
    }

}

protocol HomePeakViewControllerDelegate {
  func homePeakViewControllerUpadateActionTapped()
}

class HomePeakViewController {

  var delegate: HomePeakViewControllerDelegate?

  @available(iOS 9.0, *)
  override func previewActionItems() -> [UIPreviewActionItem] {
    let item3 = UIPreviewAction(title: "Update", style: .Default) { (action:UIPreviewAction, vc:UIViewController) -> Void in
      delegate?.homePeakViewControllerUpadateActionTapped()
    }

    return [item3]
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM