You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift 4中GLKViewController的update方法未调用问题求助

Fixing GLKViewController's update() Method Not Being Called

Hey there! I see you're dipping your toes into Swift and GLKit—awesome start! Let's get that missing update() call sorted out right away.

The Core Problem: Missing override Keyword

Looking at your code, the main issue is that you didn't mark your update() method with the override keyword. GLKViewController already has its own update() method defined, so you need to explicitly tell Swift you're replacing that parent class method. Without override, your function is just a standalone piece of code that GLKit never recognizes as the update callback it needs to trigger.

Quick Extra Checks (Just to Cover All Bases)

  • Make sure your gameController's paused property isn't accidentally set to true (it defaults to false, but it's easy to toggle by mistake). If paused is true, GLKit will stop calling update() entirely.
  • Double-check your preferredFramesPerSecond value—this controls how often update() runs. The default is 60, but if you set it to 0 or a super low number, you might not notice it firing.

Corrected Code

Here's your code with the fix applied, plus some optional sanity checks:

import GLKit
class gameController: GLKViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
        let glkView: GLKView = view as! GLKView
        glkView.context = EAGLContext(api: .openGLES2)!
        EAGLContext.setCurrent(glkView.context)
        
        // Optional: Confirm paused state (defaults to false)
        // self.paused = false
        // Optional: Adjust frame rate if needed
        // self.preferredFramesPerSecond = 60
    }
    
    // Added override here—this is the key fix!
    override func update(){}
    
    override func glkView(_ view: GLKView, drawIn rect: CGRect) {
        glClearColor(1, 1, 1, 1)
        glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
    }
}

Just adding that override keyword should get your update() method firing as expected. Give it a test, and feel free to reach out if you hit any other GLKit roadblocks!

内容的提问来源于stack exchange,提问作者victor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:15:09