gpt4 book ai didi

ios - 如何在 Swift 中终止(不是 segue)一个 View

转载 作者:行者123 更新时间:2023-11-28 07:52:00 25 4
gpt4 key购买 nike

我想知道如何在 iOS 中仅终止 1 个 View 。我正在制作一个应用程序,以便在 viewDidLoad() 中生成 52 个随机字符串 并将它们附加到一个数组中。我有它,所以当你摇动设备时,它会执行 segue。

问题在于它将项目保留在我之前提到的数组中。这意味着当用户再次进入那个 View 时,它仍然会有 52 个随机字符串,而且当它通过 viewDidLoad 函数时,它会追加 52 个字符串,所以如果用户不会终止整个应用程序,只是从 View 中跳出并返回,将有 104 个字符串而不是 52 个。

我不希望上述情况发生在我的应用程序中,所以我想知道您如何才能终止这个 View ,同时执行 segue,这样这个问题就不会发生。

这是我的应用程序中的一些代码:

这些是我的数组:

var theCardsTopPlayerHas = [""]
var theCardsBottomPlayerHas = [""]
var theCardsThatWork = ["2_of_clubs", "2_of_diamonds", "2_of_hearts", "2_of_spades", "3_of_clubs", "3_of_diamonds", "3_of_hearts", "3_of_spades", "4_of_clubs", "4_of_diamonds", "4_of_hearts", "4_of_spades", "5_of_clubs", "5_of_diamonds", "5_of_hearts", "5_of_spades", "6_of_clubs", "6_of_diamonds", "6_of_hearts", "6_of_spades", "7_of_clubs", "7_of_diamonds", "7_of_hearts", "7_of_spades", "8_of_clubs", "8_of_diamonds", "8_of_hearts", "8_of_spades", "9_of_clubs", "9_of_diamonds", "9_of_hearts", "9_of_spades", "10_of_clubs", "10_of_diamonds", "10_of_hearts", "10_of_spades", "jack_of_clubs2", "jack_of_diamonds2", "jack_of_hearts2", "jack_of_spades2", "queen_of_clubs2", "queen_of_diamonds2", "queen_of_hearts2", "queen_of_spades2", "king_of_clubs2", "king_of_diamonds2", "king_of_hearts2", "king_of_spades2","ace_of_clubs", "ace_of_diamonds", "ace_of_hearts", "ace_of_spades"]

这是 viewDidLoad 方法的内容:

struct shuffledVar {
static var shuffled = [String]();
}


override func viewDidLoad() {
super.viewDidLoad()
upperLabelNumber.transform = CGAffineTransform(rotationAngle: CGFloat.pi)

//MARK: - Shuffling Cards

for _ in 1...52 {

let shuffledCardList = Int(arc4random_uniform(UInt32(theCardsThatWork.count)))

let the_card = theCardsThatWork[shuffledCardList]

print("\(the_card)")

shuffledVar.shuffled.append(the_card)

theCardsThatWork.remove(at: shuffledCardList)

print("Finished Card")

}

theCardsTopPlayerHas = Array(shuffledVar.shuffled[0...25])
theCardsBottomPlayerHas = Array(shuffledVar.shuffled[26...51])

print("")
print(shuffledVar.shuffled)

print("")
print(theCardsTopPlayerHas)
print("")
print(theCardsBottomPlayerHas)

}

这是 segue View 的代码:

    override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {

performSegue(withIdentifier: "friendToDashboard", sender: self)

}

谢谢!

最佳答案

问题是您正在使用 static var 来存储您的字符串数组。

要解决此问题,有多种解决方案,具体取决于您的需要。这里有几个解决方案:

解决方案一

如果你真的需要shuffled数组保持为Singleto,那么你可以在viewDidLoad中清空shuffled数组 方法:

override func viewDidLoad() {
super.viewDidLoad()

shuffledVar.shuffled = []

upperLabelNumber.transform = CGAffineTransform(rotationAngle: CGFloat.pi)

...

}

方案二

如果您不需要洗牌数组保持为单例,那么您可以按如下方式修改它:

struct shuffledVar {
var shuffled = [String]();
}

var shuffled = shuffledVar()

override func viewDidLoad() {
super.viewDidLoad()
upperLabelNumber.transform = CGAffineTransform(rotationAngle: CGFloat.pi)

//MARK: - Shuffling Cards

for _ in 1...52 {

let shuffledCardList = Int(arc4random_uniform(UInt32(theCardsThatWork.count)))

let the_card = theCardsThatWork[shuffledCardList]

print("\(the_card)")

shuffled.shuffled.append(the_card)

theCardsThatWork.remove(at: shuffledCardList)

print("Finished Card")

}

theCardsTopPlayerHas = Array(shuffled.shuffled[0...25])
theCardsBottomPlayerHas = Array(shuffled.shuffled[26...51])

print("")
print(shuffled.shuffled)

print("")
print(theCardsTopPlayerHas)
print("")
print(theCardsBottomPlayerHas)
}

关于ios - 如何在 Swift 中终止(不是 segue)一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49410421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com