0

I'm trying to implement 3D Touch Peek and Pop in my swift code. 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.

Here is my code:

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

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).

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

            let test = AppointmentDetailsViewController()
            top!.presentViewController(test, animated: true, completion: {})
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

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]
  }

}
Klevison
  • 3,342
  • 2
  • 19
  • 32