【问题标题】:Multiple unwind in the same button - Swift在同一个按钮中多次展开 - Swift
【发布时间】:2015-03-02 12:03:15
【问题描述】:

我想知道是否可以在同一个按钮上分配两种不同的展开方法。例如:

我有这个视图导航:

  • 首页->A->B
  • 首页->C->B

A 和 C 视图导航到 B,但是我想使用相同的 B 视图和控制器返回到以前的视图。

有可能吗?

我一直在考虑以编程方式为按钮编写分配展开方法,具体取决于出现的视图。

提前致谢

对不起,我的英语不好。

【问题讨论】:

    标签: ios swift navigationcontroller unwind-segue


    【解决方案1】:

    这是一个对我来说效果很好的 Swift 解决方案。下面的代码只有在故事板和代码中正确连接转场时才有效。查看此page 以获得有关设置展开转场的详细说明。

    总结:

    1. 您正在从多个其他视图访问同一个视图。因此,当您选择一个视图时,您可以将源视图控制器(您当前所在的视图)传递给您将要访问的视图中的一个属性。

    2. 在您将要放松的视图中,您可以检查保存有关您来自哪里的信息(类)的属性,然后根据它的视图执行 segue。

    代码:(使用前:Home -> A -> B 或... Home -> C -> B)

    注意:B 是将展开为多个不同视图的视图。

    在 A 或 C 中:(代码在两个视图中的工作方式相同)

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "segueIdentifierInViewAthatGoesToViewB" {  
            let controller:B = segue.destinationViewController as! B
            //the code below passes our current view controller class to a property in view B. 
            //So, view B will know what view we came from. 
            //In our example, we're coming from view A or C
            controller.viewControllerNavigatedFrom = segue.sourceViewController
        }
    }
    

    在 B 中:

    //设置一个 IBAction,它将检查源视图控制器(我们在转接该视图时传递的)并根据我们来自的位置执行转接。你可以把它连接到一个按钮或任何你真正想要的东西上。

    //isKindOfClass(A) - “A”将是你的班级名称

    //setup a property to receive the view controller class where we are coming from
    var viewControllerNavigatedFrom:AnyObject?
    
    @IBAction func myButtonPressed(sender: AnyObject) {
        if self.viewControllerNavigatedFrom!.isKindOfClass(A) {
            //Unwind to view A
            performSegueWithIdentifier("unwindFromBbackToA", sender: sender)
        }
        else if self.viewControllerNavigatedFrom!.isKindOfClass(C) {
            //Unwind to view C
            performSegueWithIdentifier("unwindFromBbackToC", sender: sender)
        }
    }
    

    【讨论】:

      【解决方案2】:

      虽然,问题不是很清楚。但我能理解的是,您想要导航回之前的视图,即 B>C 或 B>A,具体取决于用户来自哪里。

      如果是这样,请检查UINavigationController。它跟踪导航历史并自动添加后退按钮。有点像我们浏览器中的后退按钮。

      这是一个教程,虽然有点老:Link

      【讨论】:

      • 我想用 unwind segue 来做,因为它在编程上更容易和更干净,但我认为我会按照 UINavigationContoller 文档所说的方式做。非常感谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 2017-11-15
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多