【问题标题】:Using dismissViewController on queue of multiple UIViewControllers with delegate在具有委托的多个 UIViewControllers 队列上使用dismissViewController
【发布时间】:2016-06-15 10:27:50
【问题描述】:

当我的应用程序第一次启动时,我正在展示 4 UIViewController 的教程。 每个 UIViewController 都有一个带有 segue 的 Button,用于呈现下一个 ViewController。 最后一个 ViewController 有一个“让我们开始”按钮,它应该完全关闭教程。

问题: 它会关闭除第一个之外的所有 ViewController。我不明白为什么?!

我的期望:

在最后一个 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)
}

但是为什么呢?我不明白背后的逻辑......

【问题讨论】:

  • 您是否尝试在导航控制器中使用这四个控制器并从 PPViewContoller 中呈现 NavController?
  • 我不想把它放在导航控制器中(因为导航栏)。
  • 你总是可以像stackoverflow.com/questions/2926914/navigation-bar-show-hide一样隐藏导航栏,通过苹果方式(导航控制器)做比长期遇到许多错误或维护母马更好的方法..
  • 谢谢 mkumar,我会试试这个。这听起来真的是一个干净的解决方案

标签: ios objective-c swift cocoa-touch uiviewcontroller


【解决方案1】:

您正在寻找错误的解决方案来解决您的问题,您需要做的是寻找 Unwind Segues。浏览本教程:https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/

【讨论】:

    【解决方案2】:

    我现在用最后一个 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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多