Segue使用Text View Delegate调用了两次

时间:2018-01-29 13:37:29

标签: ios swift textview

我正在尝试制作我的第一个iOS应用程序 - 棋盘游戏跟踪器。我正在使用textView委托选择然后显示播放器 - textViewShouldBeginEditing。当我点击它时,它有时会执行两次segue,我不知道为什么。

在第二个控制器中,当我回来时,我使用导航控制器委托方法。

带有textView的viewController的重要部分:

func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
    if textView == playersNameView {
        segueKey = "all"
    } else if textView == winnersNameView {
        segueKey = "winners"
    } else if textView == loosersNameView {
        segueKey = "loosers"
    }

    if textView == pointsView {
        performSegue(withIdentifier: "addPoints", sender: self)
    } else if textView == playersNameView || textView == loosersNameView || textView == winnersNameView {
        performSegue(withIdentifier: "choosePlayers", sender: self)
    }
    return false

}


//MARK: - Managing segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch segue.identifier {
    case "chooseGame"?:
        let controller = segue.destination as! ChooseGameViewController
        controller.gameStore = gameStore
        controller.selectedGame = selectedGame
    case "choosePlayers"?:
        let controller = segue.destination as! ChoosePlayersViewController
        controller.key = segueKey
        setAvailablePlayers()
        switch segueKey {
        case "all"?:
            controller.availablePlayers = availablePlayers
            controller.selectedPlayers = selectedPlayers
            if let game = selectedGame {
                controller.maxPlayers = game.maxNoOfPlayers
            }
        case "winners"?:
            controller.availablePlayers = availablePlayers
            controller.selectedPlayers = winners
            if let game = selectedGame {
                controller.maxPlayers = game.maxNoOfPlayers - loosers.count
            }
        case "loosers"?:
            controller.availablePlayers = availablePlayers
            controller.selectedPlayers = loosers
            if let game = selectedGame {
                controller.maxPlayers = game.maxNoOfPlayers - winners.count
            }
        default:
            preconditionFailure("Wrong segue key")
        }
    default:
        preconditionFailure("Wrong segue identifier")
    }
}

和我的第二个视图控制器 - ChoosePlayersViewController

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    if let controller = viewController as? AddMatchViewController {
        switch key {
        case "all"?:
            controller.selectedPlayers = selectedPlayers
            controller.deselectedPlayers = deselectedPlayers
            controller.setPlayersPoints()
        case "winners"?:
            controller.winners = selectedPlayers
        case "loosers"?:
            controller.loosers = selectedPlayers
        default:
            preconditionFailure("Wrong key!")
        }
        controller.viewWillAppear(true)
    }
}

修改

我现在有一个解决方法 - 使用Date我检查segue之间的时间是否大于1秒。如果它更大,那么它执行segue,如果不是 - 它不会。目前工作,但我希望更优雅的答案。

0 个答案:

没有答案