Swift 4中GLKViewController的update方法未调用问题求助
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'spausedproperty isn't accidentally set totrue(it defaults tofalse, but it's easy to toggle by mistake). Ifpausedis true, GLKit will stop callingupdate()entirely. - Double-check your
preferredFramesPerSecondvalue—this controls how oftenupdate()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

