Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法关闭NavigationController_Ios_Swift_Uiviewcontroller_Uinavigationcontroller_Uipresentationcontroller - Fatal编程技术网

Ios 无法关闭NavigationController

Ios 无法关闭NavigationController,ios,swift,uiviewcontroller,uinavigationcontroller,uipresentationcontroller,Ios,Swift,Uiviewcontroller,Uinavigationcontroller,Uipresentationcontroller,大家好,我在关闭导航控制器时遇到问题。 这段代码对我不起作用。当我尝试在ViewController中调用它时 @IBAction func backButtonPressed(_ sender: UIBarButtonItem) { self.navigationController?.dismiss(animated: true, completion: nil) } 这就是我在第一个ViewController中所做的,我只是打开了ProfileViewCo

大家好,我在关闭导航控制器时遇到问题。 这段代码对我不起作用。当我尝试在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将不会关闭和冻结。有什么建议吗(

我假设您只是想关闭ViewController,而不是导航控制器本身

@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. 
}
将导航控制器视为处理一组视图。当您在导航控制器中切换到一个新视图时,您将向堆栈中添加一个新视图。当您关闭时,您将从堆栈中弹出一个视图。此外,永远不要切换到返回,始终关闭。如果您未能关闭堆栈上的视图控制器,则视图控制器将保持在m中埃默里

如果您需要将数据传递回上一个视图,一种技术称为“展开序列”


希望这有帮助!

您可以使用
navigationController?隐藏导航栏。setNavigationBarHidden(true,动画:false)
我不想隐藏它,我想取消它。您的iAction位于哪个文件中?在我的ViewControlleri中,是ProfileView还是您试图取消的带有后退按钮的ProfileView?在这种情况下,iAction需要在ProfileViewController中。iAction是否实际启动?您是否将其链接到故事板中的按钮?它是它已经在ProfileViewConrtoleller.是的,它正在开火