swift - View Controller 被关闭后执行segue

标签 swift interface-builder segue

我正在使用 UINavigationController。我想显示一个中间屏幕,例如。白色,然后从那里我想放弃并转向绿色。

我不创建从白色到绿色的过渡的原因是,在用户返回的情况下,他们应该返回到蓝色,因为蓝色是我的主屏幕。

hurray 代码如下:

class BlueViewController: UIViewController {
    @IBAction func tapBlue(_ sender: Any) {
        self.performSegue(withIdentifier: "whiteSegue", sender: self)
    }
}

class WhiteViewController: UIViewController {
    @IBAction func tapGreen(_ sender: Any) {
        navigationController?.popViewController(animated: true)
        weak var pvc = self.presentingViewController
        dismiss(animated: true){
            pvc?.performSegue(withIdentifier: "greenSegue", sender: self)
        }
    }
}

这是代码库

https://github.com/omenking/DismissAndSegue

没有发生错误,但是当白色被消除时,它不会变成绿色。 我知道之前在 StackOverflow 上已经有人问过这个问题,但其他示例不起作用或者对于最新的 iOS 来说已经过时了。

最佳答案

主要问题是,由于您使用导航 View Controller 并将 View Controller 插入和推出堆栈,因此 self.presentingViewController 变量将为 nil。它用于模式演示,而不是导航 View Controller 。

试试这个:

class WhiteViewController: UIViewController {
    @IBAction func tapGreen(_ sender: Any) {
        // Get navigation stack, remove last item (White VC)
        var viewControllers = navigationController?.viewControllers
        viewControllers.removeLast()

        // Instantiate new Green VC from storyboard
        let storyboard = UIStoryboard(name: "Main", bundle: nil) //Change bundle name
        let greenViewController = storyboard.instantiateViewController(withIdentifier: "GreenViewController") //Change storyboard ID
        viewControllers.append(greenViewController)

        // Perform the transition to Green VC with animation
        navigationController?.setViewControllers(viewControllers, animated: true)
    }
}

关于swift - View Controller 被关闭后执行segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49989151/

相关文章:

swift - 无法使用完成 block 推断静态函数的泛型类型

ios - 添加到代码时按钮的@IBActions 断开连接 - Swift,Xcode 8

ios - 检查 segue 标识符而不是使用失败的类型转换有好处吗?

objective-c - 从自定义类访问 IB 对象

ios - Xcode - 如何修复 'NSUnknownKeyException' ,原因 : … this class is not key value coding-compliant for the key X"error?

部分屏幕区域中类似 iOS UINavigationController 的行为 (2016)

ios - 无法从另一个 TableView 加载另一个 TableView

ios - 自定义 UICollectionView 数据源和委托(delegate)

swift - 我想限制点击次数

ios - 保护 IOS 应用免受 Lucky Patcher 和其他此类应用的侵害