iOS app written in swift is crashing when returning to a view -


i'm writing app loads options screen (scene 1) user fill in text fields segue new scene (scene 2). after user finished scene 2, user can click button segue them scene 1 fill out options again. on scene 1, i'm setting first text field become first responder keyboard automatically appears when view loads.

override func viewdidload() {     super.viewdidload()     self.numeratorbegin.becomefirstresponder()      // additional setup after loading view. } 

this works great when app loads. keyboard appears , cursor in numeratorbegin text field. however, when user finishes scene 2 , presses button go scene 1, app crashes. crash not occur when first responder not being set in viewdidload. debugger shows following line causing crash message thread 1: exc_bad_access(code=2, address=hexhere)

class appdelegate: uiresponder, uiapplicationdelegate { 

the idea have numeratorbegin text field become first responder every time view loads. life of me can't discover why app crashing.

adding more points alain t.'s answer.

instead of

self.numeratorbegin.becomefirstresponder() inside override func viewdidload()

add override func viewwillappear()

and self.numeratorbegin.resignfirstresponder() inside override func viewwilldisappear()

if try use becomefirstresponder()/resignfirstresponder() in other thread, causes crash. it's better follow above methods executes in main thread.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -