ios - 快速 3D Peek 和 POP

标签 ios swift 3dtouch peek

我正在尝试在我的 swift 代码中实现 3D Touch Peek 和 Pop。当用户在预览 View 上按得更深时,将出现一系列预览操作(共享、更新、删除)。

我需要的是当用户选择“更新”操作时将移动到 UpdateView Controller ,但它不断崩溃。

这是我的代码:

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)

HomeViewController.swift 中的 POP 方法

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

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

    }

我也尝试使用此代码移动到更新屏幕,但它给了我( fatal error :在展开可选值时意外发现 nil)。

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

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

最佳答案

也许您需要处理委托(delegate):

例如:

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

}

关于ios - 快速 3D Peek 和 POP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36152879/

相关文章:

iphone - 我可以将 2 个 nib 文件连接到同一个 .h 和 .m 吗?

ios - EXC_BAD_ACCESS(代码=1,地址=0x0)

swift - Realm 按日期列表内的日期对对象进行排序

ios - 'title' 的类型与协议(protocol) 'UIPreviewActionItem' 要求的可选性不同

iphone - 无需向 Apple 支付 99 美元即可在 iPhone 上测试应用程序

ios - 使用终端命令 'open -n -a "iOS Simulator"' 运行多个 iOS Simulator.app

ios - 我应该把按钮的 Action 放在 UITableViewDelegate 的什么地方

ios - 在我的应用程序中使用 3D Touch

ios - 3D触摸快捷方式应用程序:didfinishlaunchingwithoptions:performActionForShortcutItem:执行顺序

ios - 如何使用 MonoTouch AudioConverter 将 LinearPCM 转换为压缩格式?