在命令上连续加载CustomViewControllers

时间:2014-11-23 04:27:58

标签: ios swift viewcontroller

我想连续加载两个自定义视图控制器(按下第一个下一个按钮)

    let aVc: UIViewController = StartAlertViewVC()
    self.presentViewController(aVc, animated: true, completion: nil)

此代码来自我的主控制器查看控制器。 这就是我调用第一个自定义视图控制器( StartAlertViewVC )的方式。

在StartAlertViewVC.swift上,我有一个下一个按钮。 (只是简单地驳回了它)

@IBAction func Next(sender: AnyObject) {
    let selectedStartTime: String = startTimeLabel.text!
    self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
}

基本上,当在StartAlertViewVC上按下“下一步”按钮时,我希望它将其关闭,然后加载我的第二个自定义视图控制器( EndAlertViewVC)。在该控制器上,有一个显示“报告!”的按钮。

我希望它可以像这样工作:

ViewController.swift

let aVc: UIViewController = StartAlertViewVC()
self.presentViewController(aVc, animated: true, completion: nil)

// If "next" button is pressed, then load EndAlertViewVC

let eVc: UIViewController = EndAlertViewVC()
self.presentViewController(aVc, animated: true, completion: nil)

// If "report!" button is pressed, then load ...

segue.performSegueWithIdentifier("report", sender: AnyObject?())

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

有多种方法可以实现这一目标。这是一个简单的解决方案:

当模拟呈现的AlertVC被解雇时,知道viewWillAppear将会ViewController被调用...

class ViewController: UIViewController {

var shouldPresentEndAlertVC = false

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    // This bool is set to true only when the StartAlertVC is presented
    if shouldPresentEndAlertVC == true {

        let eVc: UIViewController = EndAlertViewVC()
        self.presentViewController(eVc, animated: true, completion: nil)
        shouldPresentEndAlertVC = false
    }

}

func showStartAlertController(){
    let aVc: UIViewController = StartAlertViewVC()
    self.presentViewController(aVc, animated: true, completion: nil)
    shouldPresentEndAlertVC = true
}
}

另一个解决方案是让ViewController处理两个alertVC的解雇,(使用alertVC上的自定义委托,与{{{{}进行通信1}})

ViewController上的此委托方法可能看起来像这样

ViewController