Swift:从不同的视图控制器委托调用segue

时间:2015-04-25 22:45:12

标签: swift segue

我有一个viewcontroller,我从中调用一个popover,在popover中我有一个委托,它在主viewcontroller中调用一个函数。

在此功能中,我关闭弹出窗口并想要加载不同的视图控制器,

如果我这样做,

关闭原始的popover工作

func KeypadDismissData(View: UIViewController, Password : String){
    View.dismissViewControllerAnimated(true, completion: nil)  
} 

但是,如果我尝试调用segue来加载带有代码的新视图控制器

func KeypadDismissData(View: UIViewController, Password : String){
    View.dismissViewControllerAnimated(true, completion: nil)
    self.performSegueWithIdentifier("LoginToMain", sender:nil)
}

我收到错误

Warning: Attempt to present <UIViewController: 0x7fc768488900> on ... whose view is not in the window hierarchy!

我有什么想法可以绕过这个?

由于

1 个答案:

答案 0 :(得分:0)

不确定我是否解决了您的问题,但我可以尝试:

您在MainViewController中打开一个弹出窗口。要关闭此弹出窗口,请使用委托,在MainViewController中解除此弹出窗口。在弹出窗口被解除后,您想要转到另一个视图。

我重新创建了它,代码如下:

MainViewController:

protocol DismissDelegate: class {
    func dismiss()
}

class ViewController: UIViewController, DismissDelegate {

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

    @IBAction func openButton(sender: UIButton) {
        let popoverViewController = storyboard?.instantiateViewControllerWithIdentifier("popoverViewController") as! PopoverViewController
        popoverViewController.modalPresentationStyle = .Popover
        popoverViewController.delegate = self
        popoverViewController.preferredContentSize = CGSize(width: 200, height: 260)
        popoverViewController.view.backgroundColor = UIColor.yellowColor()

        // Settings for the popover
        let popover = popoverViewController.popoverPresentationController!
        popover.sourceView = self.view
        popover.sourceRect = sender.frame
        popover.backgroundColor = UIColor.redColor()

        presentViewController(popoverViewController, animated: true, completion: nil)
    }

    func dismiss() {
        dismissViewControllerAnimated(true) {
            self.performSegueWithIdentifier("segue", sender: self)
        }
    }
}

PopoverViewController:

class PopoverViewController: UIViewController {

    weak var delegate: DismissDelegate?

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

    @IBAction func dismissButton(sender: UIButton) {
        delegate?.dismiss()
    }
}

不同之处在于,我已将segue完成dismissViewControllerAnimated