在视图控制器被调暗后执行segue

时间:2018-04-23 20:06:12

标签: swift interface-builder segue

我正在使用UINavigationController。我想展示一个中间屏幕,例如。白色然后从那里我想要dimiss并且变成绿色。

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

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已经过时了。

3 个答案:

答案 0 :(得分:0)

主要问题是,由于您正在使用导航视图控制器并在视图堆栈上打开视图控制器,因此self.presentingViewController变量将为零。这用于模态演示,而不是导航视图控制器。

试试这个:

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

答案 1 :(得分:0)

稍微不同的解决方案是链接您的视图控制器SELECT salaryProcessID, GROUP_CONCAT(description) descr_list1, GROUP_CONCAT(description ORDER BY description SEPARATOR '; ') descr_list2 FROM hrms_emp_type AS spTB JOIN employment_types AS empType ON empType.id = spTB.empType GROUP BY salaryProcessID ORDER BY salaryProcessID - > blue - > white,然后在绿色视图控制器中删除{{1}从导航堆栈中查看控制器。

您的green视图控制器就变得如此简单。

white

其他视图控制器无需特殊处理。

答案 2 :(得分:0)

我这样做,在白色控制器中

     performSegue(withIdentifier: "showSchedule", sender: date)
     if let count = self.navigationController?.viewControllers.count {
           self.navigationController?.viewControllers.remove(at: count - 2)
      }
相关问题