检查哪个ViewController创建了一个segue(来自目标ViewController)

时间:2016-06-25 15:59:03

标签: ios arrays swift uiviewcontroller segue

我有一个代码可以为我的12个ViewControllers提供随机segue。

在ViewController1中,它看起来像这样;

let segues = ["1-2", "1-3", "1-4", "1-5", "1-6", "1-7", "1-8", "1-9", "1-10", "1-11", "1-12"]
        let index = Int(arc4random_uniform(UInt32(segues.count)))
        let segueName = segues[index]
        self.performSegueWithIdentifier(segueName, sender: self)

在ViewController 2中它看起来一样,但是segue名称变为;

let segues = ["2-1", "2-3", "2-4", "2-5", "2-6", "2-7", "2-8", "2-9", "2-10", "2-11", "2-12"]

"1"更改为"2") - 依此类推,适用于所有12个ViewControllers。

现在,我要做的是删除已经显示的ViewControllers,以便下一个segue不会返回任何以前的ViewControllers。

示例:

•ViewController1对ViewController2("1-2"

进行了segue

•ViewController2从数组"2-1"删除segue segues

•ViewController2然后对ViewController3 "1-3"

进行segue

•ViewController3删除segue "3-1""3-2"

依旧......

2 个答案:

答案 0 :(得分:1)

我强烈怀疑你的主逻辑可以被制作成更易于管理的东西,但是在目前的设置下,我建议单独SegueCoordinator发送下一个segue直到它耗尽(2 -11,11-3,3-6,6-4 ......)等:

import GameplayKit

class SegueCoordinator {
    private var numberOfViewControllers = 0
    private var segues = [String]()

    init(numberOfViewControllers: Int) {
        self.numberOfViewControllers = numberOfViewControllers
        setupSegues()
    }

    func nextSegue() -> String? {
        let segue = segues.first
        segues.removeFirst()
        return segue
    }

    private func setupSegues() {
        let allIds = Array(1...numberOfViewControllers)
        var shuffleIds = GKRandomSource().arrayByShufflingObjectsInArray(allIds) as! [Int]
        while shuffleIds.count > 1 {
            let availableIds = shuffleIds.filter{$0 != shuffleIds.first!}
            let segue = "\(shuffleIds.first!)-\(availableIds.first!)"
            segues.append(segue)
            shuffleIds.removeFirst()
        }
    }
} 

然后,您将从您想要启动的任何ViewController创建协调器:let coordinator = SegueCoordinator(numberOfViewControllers: 12)(这可以很容易地更改为从协调器本身中选择一个随机的firstControler。)

然后向协调员询问下一个观点:

let segue = coordinator.nextSegue()

将协调器传递给prepareForSegue中的nextVC。

答案 1 :(得分:1)

这是一种方法:

  1. 将您访问过的viewControllers数组作为每个viewControllers中的属性。我们称之为visited。此外,创建一个名为TracksVisited协议,您的所有viewControllers都将采用该协议

    protocol TracksVisited {
        var visited: [Int] { get set }
    }
    
    class ViewController1: UIViewController, TracksVisited {
        var visited = [Int]()
    
        ...
    }
    
    class ViewController2: UIViewController, TracksVisited {
        var visited = [Int]()
    
        ...
    }
    
  2. 将当前的viewController编号添加到此列表中:

    let vc = 3  // current viewController
    visited.append(vc)
    
  3. 生成segues列表并选择一个:

    // generate list of all viewControllers
    let all = Array(1...12)
    
    // remove visited from all
    let rest = all.filter { !visited.contains($0) }
    
    // create list of segues
    let segues = rest.map { "\(vc)-\($0)" }
    
    // choose random segue and go there
    let index = Int(arc4random_uniform(UInt32(segues.count)))
    let segueName = segues[index]
    self.performSegueWithIdentifier(segueName, sender: self)
    
  4. prepareForSegue中,将visited列表传递给目标viewController:

    if let destinationViewController = segue.destinationViewController as? TracksVisited {
        destinationViewController.visited = visited
    }