当我的应用程序第一次启动时,我正在展示4个UIViewController的教程。 每个UIViewController都有一个带有segue的Button,呈现下一个ViewController。 最后一个ViewController有一个按钮"让我们开始"这应该完全取消教程。
问题: 它解除了除第一个之外的所有ViewControllers。我不明白为什么?!
我的期望:
在最后一个ViewController4上我调用了第一个ViewController的dismissIntroduction()函数,所以我除了所有ViewControllers(包含ViewController1)之外都应该消失。 当我在第一个ViewController上放一个按钮并调用函数" dismissIntroduction()"它消失了。
ViewController 1(WelcomeViewController):
protocol WelcomeViewDelegate {
func dismissIntroduction()
}
class WelcomeViewController: UIViewController, WelcomeViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func dismissIntroduction() {
self.dismissViewControllerAnimated(true, completion: nil)
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destination = segue.destinationViewController as! ViewController2
destination.delegate = self
}
}
ViewController 2:
class ViewController2: UIViewController {
var delegate:WelcomeViewDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destination = segue.destinationViewController as! ViewController3
destination.delegate = self.delegate
}
}
ViewController 3:
class ViewController3: UIViewController {
var delegate:WelcomeViewDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destination = segue.destinationViewController as! ViewController4
destination.delegate = self.delegate
}
}
ViewController4(最后一个):
class ViewController4: UIViewController {
var delegate:WelcomeViewDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func pressLetsStart(sender: AnyObject) {
self.delegate!.dismissIntroduction()
}
}
编辑: 当我把dismissViewControllerAnimated函数放两次时,我得到了它的工作!?
func dismissIntroduction() {
self.dismissViewControllerAnimated(true, completion: nil)
self.dismissViewControllerAnimated(true, completion: nil)
}
但为什么呢?我不明白背后的逻辑......
答案 0 :(得分:0)
您正在寻找解决问题的错误方法,您需要做的是寻找Unwind Segues。完成本教程:https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/
答案 1 :(得分:0)
我现在用最后一个ViewController中的以下函数解决了这个问题:
@IBAction func pressLetsStart(sender: AnyObject) {
self.dismissModalStack()
}
private func dismissModalStack() {
var vc = self.presentingViewController! as UIViewController
while (vc.presentingViewController != nil) {
vc = vc.presentingViewController!;
}
vc.dismissViewControllerAnimated(true, completion: nil)
}