-1

大家好,我在关闭 NavigationController 时遇到问题。这段代码对我不起作用。当我尝试在我的 ViewController 中调用它时

   @IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
        self.navigationController?.dismiss(animated: true, completion: nil)
    }

这就是我在我的第一个 ViewController 中所做的,我只需打开具有自定义转换的 ProfileViewController。

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "Profile") as? NavigationControllerTransition
controller?.modalPresentationStyle = .custom
controller?.transitioningDelegate = self
controller?.interactor = self.interactor
controller?.animatorDirection.direction = .right
self.animatedTransition.animatorDirection.direction = .right
self.present(controller!, animated: true, completion: {})

我有一个自定义 NavigationController 类,它调用 NavigationControllerTransition 以使其碰巧从屏幕边缘滑动以进行自定义转换。

class NavigationControllerTransition: UINavigationController {

    var interactor: Interactor?
    var animatorDirection = AnimatorDirection()

    override func viewDidLoad() {
        super.viewDidLoad()

        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(NavigationControllerTransition.handleTap(sender:)))
        self.view.addGestureRecognizer(panGesture)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func handleTap(sender: UIPanGestureRecognizer) {

        let percentThreshold: CGFloat = 0.3
        let translation = sender.translation(in: view)

        var horitonzalMovement: CGFloat = 0.0

        var downwardMovementPercent: Float = 0.0

        if self.animatorDirection.direction == .left {
            horitonzalMovement = -(translation.x / view.bounds.width)
            let downwardMovement = fmaxf(Float(horitonzalMovement), 0.0)
            downwardMovementPercent = fminf(downwardMovement, 1.0)

        } else {
            horitonzalMovement = +(translation.x / view.bounds.width)
            let downwardMovement = fmaxf(Float(horitonzalMovement), 0.0)
            downwardMovementPercent = fminf(downwardMovement, 1.0)

        }

        let progress = CGFloat(downwardMovementPercent)
        guard let interactor = interactor else { return }

        switch sender.state {
        case .began:
            interactor.hasStarted = true
            dismiss(animated: true, completion: nil)
        case .changed:
            interactor.shouldFinish = progress > percentThreshold
            interactor.update(progress)
        case .cancelled:
            interactor.hasStarted = false
            interactor.cancel()
        case .ended:
            interactor.hasStarted = false
            interactor.shouldFinish ? interactor.finish() : interactor.cancel()
        default: break
        }
    }
}

所以我可以滑动来打开或关闭我的 ViewController 并且过渡效果很好。唯一的问题是当我尝试使用 UIButton 关闭 ViewController 时。NavigationController 不会关闭和冻结。有小费吗 ?:(

4

1 回答 1

1

我假设您只是想关闭 ViewController 而不是导航控制器本身。在这种情况下,您的 IBAction 应该如下所示。

@IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
    self.dismissViewControllerAnimated(true, completion: nil)
    // Technically you don't even need the self here. 
    // In swift self is implicit in most cases. 
}

将导航控制器视为处理一堆视图。当您在导航控制器中切换到一个新视图时,您将一个新视图添加到堆栈中。当您解雇时,您会从堆栈中弹出一个。也从不追究回去,总是解雇。如果您未能关闭堆栈上的视图控制器,则视图控制器将仅保留在内存中。

这是关于这个主题的一个很好的教程。

如果您需要将数据传递回前一个视图,则一种技术称为展开转场。在这里阅读。

希望这可以帮助!

于 2016-07-16T18:03:04.303 回答