ios - Extension for view controller using Bool in Swift -


i'm try make extension view controller. simple view tap recognizer. if pushed once, background color should change magenta orange. next push should magenta again.

if concrete vc, let say, startvc, works. here code:

import uikit  class startvc: uiviewcontroller {     // bool     var colorchanged = false      override func viewdidload() {         super.viewdidload()          view.backgroundcolor = uicolor.magenta         setmytouchableview2()     } }  extension startvc {     // make touchable view     func setmytouchableview2() {         let myview = uiimageview(frame: cgrect(x: 100, y: 100, width: 50, height: 50))         myview.backgroundcolor = uicolor.yellow          // add gesture recognizer         let tapgesturerecognizer = uitapgesturerecognizer(target: self, action: #selector(changecolor2(_:)))         myview.addgesturerecognizer(tapgesturerecognizer)         myview.isuserinteractionenabled = true          view.addsubview(myview)     }      func changecolor2(_ recognizer:uitapgesturerecognizer){         if(colorchanged) {             view.backgroundcolor = uicolor.magenta         } else {             view.backgroundcolor = uicolor.orange         }          colorchanged = !colorchanged     } } 

but if try generalize extension view controller, following problem: cant put boolean (colorchanged) inside view controller because extension not see it. how can solve it?

here code:

import uikit  class viewcontroller: uiviewcontroller {     // bool     var colorchanged = false      override func viewdidload() {         super.viewdidload()          view.backgroundcolor = uicolor.magenta         setmytouchableview()     } }  extension uiviewcontroller {     // make touchable view     func setmytouchableview() {         let myview = uiimageview(frame: cgrect(x: 100, y: 100, width: 50, height: 50))         myview.backgroundcolor = uicolor.yellow          // add gesture recognizer         let tapgesturerecognizer = uitapgesturerecognizer(target: self, action: #selector(changecolor(_:)))         myview.addgesturerecognizer(tapgesturerecognizer)         myview.isuserinteractionenabled = true          view.addsubview(myview)     }      func changecolor(_ recognizer:uitapgesturerecognizer){         if(colorchanged) {             view.backgroundcolor = uicolor.magenta         } else {             view.backgroundcolor = uicolor.orange         }     } 

you can use struct make global variable -

   struct globalstruct {     static var colorchanged = false }  class viewcontroller: uiviewcontroller {      override func viewdidload() {         super.viewdidload()          view.backgroundcolor = uicolor.magenta         setmytouchableview()     } }  extension uiviewcontroller {     // make touchable view     func setmytouchableview() {         let myview = uiimageview(frame: cgrect(x: 100, y: 100, width: 50, height: 50))         myview.backgroundcolor = uicolor.yellow          // add gesture recognizer         let tapgesturerecognizer = uitapgesturerecognizer(target: self, action: #selector(changecolor(_:)))         myview.addgesturerecognizer(tapgesturerecognizer)         myview.isuserinteractionenabled = true          view.addsubview(myview)     }      func changecolor(_ recognizer:uitapgesturerecognizer){         if(globalstruct.colorchanged) {             view.backgroundcolor = uicolor.magenta         } else {             view.backgroundcolor = uicolor.orange         }     } } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -