为啥使用@IBAction func时UI没有及时更新?

Posted

技术标签:

【中文标题】为啥使用@IBAction func时UI没有及时更新?【英文标题】:Why didn't the UI update timely when using @IBAction func?为什么使用@IBAction func时UI没有及时更新? 【发布时间】:2019-03-07 02:12:46 【问题描述】:

这是@IBAction func的代码

@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 func touchCard(_ sender: 中的 updateViewFromModel() UIButton) 然后我点击了 Xcode 的“运行”按钮 iPhone 模拟器出来了。 the default UI image without any clicking 我在第一行从左到右点击了第一张“卡片”(实际上是一个按钮) 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应该会变,但是为什么UI没有及时更新呢? 我在 func updateViewFromModel 中完成了左侧代码的执行,因为我没有点击任何其他卡片。 Finally it came to the end of @IBAction func touchCard,用户界面仍然保持默认状态。 我点击了“继续执行程序”按钮,the UI responded correctly。我觉得很奇怪。

我想弄清楚的是,在第 9 步,为什么 UI 没有及时更新。

非常感谢您的帮助!

【问题讨论】:

【参考方案1】:

当您对视图进行更改时,它会等待重绘周期,因此视图不会立即更新。 IBAction 没有什么问题,你把改动放在别处并设置断点,不会有任何区别。

【讨论】:

【参考方案2】:

这可能是由于模拟器速度慢,因为我也面临模拟器速度慢的问题,我不知道其他人是否也遇到过这个问题,我怀疑你是否面临这个问题,但我不确定。

1- 使用 Xcode 9.2 和 ios11 模拟器时,您的代码更新模拟器没有任何问题。

2- 在您的设备上试一试

【讨论】:

我刚刚在我的设备上试了一下。它显示与模拟器相同的结果。 是的!我的设备是Xs,和模拟器一样。 当你做 IBACtion 时,还有其他事情在处理吗?

以上是关于为啥使用@IBAction func时UI没有及时更新?的主要内容,如果未能解决你的问题,请参考以下文章

IBAction 的不同触摸状态

如何将@IBAction func 数据放入 TableView?

未调用 IBAction,但 UI 正在更新?

为啥我的代码中的 SQL 命令在运行时被“TODO: FUNC”替换?

为啥func调用中的代码块没有被执行?

为啥静态字段没有及时初始化?