为什么使用@IBAction func 时UI 没有及时更新?
Why didn't the UI update timely when using @IBAction func?
这是@IBAction函数的代码
@IBAction func touchCard(_ sender: UIButton) {
flipCount += 1
if let index = buttonCollection.firstIndex(of: sender) {
game.chooseCard(at: index)
updateViewFromModel() // I set breakpoint at here
}
}
Concentration.swift 文件,MVC 模型的一部分
class Concentration {
var cards = [Card]()
var numberOfPairsOfCards: Int
var identifierOfOneAndOnlyOneCard: Int? {
didSet {
print("identifierOfOneAndOnlyOneCard: \(identifierOfOneAndOnlyOneCard)")
}
}
init(numberOfPairsOfCards: Int) {
self.numberOfPairsOfCards = numberOfPairsOfCards
for _ in 0..<numberOfPairsOfCards {
let card = Card()
cards += [card, card]
}
}
func chooseCard(at Index: Int) {
print("Index: \(Index)")
if !cards[Index].isMatched {
if let matchIndex = identifierOfOneAndOnlyOneCard, matchIndex != Index {
// check if cards match
if cards[matchIndex].identifier == cards[Index].identifier {
cards[matchIndex].isMatched = true
cards[Index].isMatched = true
}
cards[Index].isFaceUp = true
identifierOfOneAndOnlyOneCard = nil
} else {
// either no cards or 2 cards are face up
for flipDownIndex in cards.indices {
cards[flipDownIndex].isFaceUp = false
}
cards[Index].isFaceUp = true
identifierOfOneAndOnlyOneCard = Index
}
}
}
}
the code of func updateViewFromModel()
Card.swift 文件,MVC 模型的一部分
struct Card {
var isFaceUp: Bool = false
var isMatched: Bool = false
var identifier = getUniqueIdentifier()
static var uniqueIdentifier: Int = 0
static func getUniqueIdentifier() -> Int{
uniqueIdentifier += 1
return uniqueIdentifier
}
}
这些代码是来自 CS193p 的项目集中游戏的一部分。
当我一步步跟踪代码时,我发现了一些令人费解的地方。
- 如前所述,我在
@IBAction 中的 updateViewFromModel() func touchCard(_ sender:
UI按钮)
- 然后我点击了Xcode的'Run'按钮
- iPhone模拟器出来了。
- the default UI image without any clicking
- 我点击了第一行从左到右的第一个'card'(实际上是一个按钮)
- Xcode reacted 和 UI 与默认值相同
- 我开始使用 LLDB 调试代码,然后进入 func updateViewFromModel()
- When I stepped over to Line 64,显示第一张卡片的isFaceUp是真的,因为我刚点了这张卡片。
- 我们继续,I stepped over to Line 68。必须执行第65和66行!我的想法是,在执行第 65 和 66 行时,
UI 应该是 change.But,为什么 UI 没有及时更新?
- 我在 func updateViewFromModel 中执行完了左边的代码,因为我没有点击任何其他卡片。
- Finally it came to the end of @IBAction func touchCard,UI还是默认的
- 我点击了'continue program execution'按钮,the UI responded correctly。我觉得很奇怪。
我想弄明白的是在第9步为什么UI没有及时更新。
非常感谢您的帮助!
当您对视图进行更改时,它会等待重绘周期,因此视图不会立即更新。 IBAction 没有问题,你把修改放在别处并设置一个断点,它不会有任何区别。
这可能是由于模拟器速度慢,因为我也面临着模拟器速度慢的问题我不知道其他人是否也遇到过这个问题,我怀疑你是否遇到过这个问题但我不确定。
1- 使用 Xcode 9.2 和 IOS11.
的模拟器时,您的代码更新模拟器没有任何问题
2- 在您的设备上试一试
这是@IBAction函数的代码
@IBAction func touchCard(_ sender: UIButton) {
flipCount += 1
if let index = buttonCollection.firstIndex(of: sender) {
game.chooseCard(at: index)
updateViewFromModel() // I set breakpoint at here
}
}
Concentration.swift 文件,MVC 模型的一部分
class Concentration {
var cards = [Card]()
var numberOfPairsOfCards: Int
var identifierOfOneAndOnlyOneCard: Int? {
didSet {
print("identifierOfOneAndOnlyOneCard: \(identifierOfOneAndOnlyOneCard)")
}
}
init(numberOfPairsOfCards: Int) {
self.numberOfPairsOfCards = numberOfPairsOfCards
for _ in 0..<numberOfPairsOfCards {
let card = Card()
cards += [card, card]
}
}
func chooseCard(at Index: Int) {
print("Index: \(Index)")
if !cards[Index].isMatched {
if let matchIndex = identifierOfOneAndOnlyOneCard, matchIndex != Index {
// check if cards match
if cards[matchIndex].identifier == cards[Index].identifier {
cards[matchIndex].isMatched = true
cards[Index].isMatched = true
}
cards[Index].isFaceUp = true
identifierOfOneAndOnlyOneCard = nil
} else {
// either no cards or 2 cards are face up
for flipDownIndex in cards.indices {
cards[flipDownIndex].isFaceUp = false
}
cards[Index].isFaceUp = true
identifierOfOneAndOnlyOneCard = Index
}
}
}
}
the code of func updateViewFromModel()
Card.swift 文件,MVC 模型的一部分
struct Card {
var isFaceUp: Bool = false
var isMatched: Bool = false
var identifier = getUniqueIdentifier()
static var uniqueIdentifier: Int = 0
static func getUniqueIdentifier() -> Int{
uniqueIdentifier += 1
return uniqueIdentifier
}
}
这些代码是来自 CS193p 的项目集中游戏的一部分。
当我一步步跟踪代码时,我发现了一些令人费解的地方。
- 如前所述,我在 @IBAction 中的 updateViewFromModel() func touchCard(_ sender: UI按钮)
- 然后我点击了Xcode的'Run'按钮
- iPhone模拟器出来了。
- the default UI image without any clicking
- 我点击了第一行从左到右的第一个'card'(实际上是一个按钮)
- Xcode reacted 和 UI 与默认值相同
- 我开始使用 LLDB 调试代码,然后进入 func updateViewFromModel()
- When I stepped over to Line 64,显示第一张卡片的isFaceUp是真的,因为我刚点了这张卡片。
- 我们继续,I stepped over to Line 68。必须执行第65和66行!我的想法是,在执行第 65 和 66 行时, UI 应该是 change.But,为什么 UI 没有及时更新?
- 我在 func updateViewFromModel 中执行完了左边的代码,因为我没有点击任何其他卡片。
- Finally it came to the end of @IBAction func touchCard,UI还是默认的
- 我点击了'continue program execution'按钮,the UI responded correctly。我觉得很奇怪。
我想弄明白的是在第9步为什么UI没有及时更新。
非常感谢您的帮助!
当您对视图进行更改时,它会等待重绘周期,因此视图不会立即更新。 IBAction 没有问题,你把修改放在别处并设置一个断点,它不会有任何区别。
这可能是由于模拟器速度慢,因为我也面临着模拟器速度慢的问题我不知道其他人是否也遇到过这个问题,我怀疑你是否遇到过这个问题但我不确定。
1- 使用 Xcode 9.2 和 IOS11.
的模拟器时,您的代码更新模拟器没有任何问题2- 在您的设备上试一试