ios - use activity indicator in many VC without duplicating code swift -
i have 2 viewcontrollers
(a , b) in swift ios. both , b loads data internet (separately). want display activityindicator
while loading. know can bad way declaring once in each vc follows
viewcontroller a
var activityindicator: uiactivityindicatorview = uiactivityindicatorview() func activityindicatorbegin() { activityindicator = uiactivityindicatorview(frame: cgrectmake(0,0,50,50)) activityindicator.center = self.view.center activityindicator.hideswhenstopped = true activityindicator.activityindicatorviewstyle = uiactivityindicatorviewstyle.gray view.addsubview(activityindicator) activityindicator.startanimating() disableuserinteraction() greyview = uiview() greyview.frame = cgrectmake(0, 0, self.view.bounds.width, self.view.bounds.height) greyview.backgroundcolor = uicolor.blackcolor() greyview.alpha = 0.5 self.view.addsubview(greyview) } func activityindicatorend() { self.activityindicator.stopanimating() enableuserinteraction() self.greyview.removefromsuperview() }
and exact same viewcontroller b , call activityindicatorbegin , activityindicatorend functions declared in b view controller. however, want make code cleaner. how done? trying make code cleaner these days.
thanks,
--update--
i imageing following code work. doesnt because cant declare variables in extensions
var activityindicator: uiactivityindicatorview = uiactivityindicatorview() extension uiviewcontroller { func activityindicatorbegin() { activityindicator = uiactivityindicatorview(frame: cgrectmake(0,0,50,50)) activityindicator.center = self.view.center activityindicator.hideswhenstopped = true activityindicator.activityindicatorviewstyle = uiactivityindicatorviewstyle.gray view.addsubview(activityindicator) activityindicator.startanimating() disableuserinteraction() greyview = uiview() greyview.frame = cgrectmake(0, 0, self.view.bounds.width, self.view.bounds.height) greyview.backgroundcolor = uicolor.blackcolor() greyview.alpha = 0.5 self.view.addsubview(greyview) } func activityindicatorend() { self.activityindicator.stopanimating() enableuserinteraction() self.greyview.removefromsuperview() } }
create 1 baseviewcontroller
, add these 2 method , declare activityindicator
object inside baseviewcontroller
. make baseviewcontroller
parent class of viewcontroller
. after if want add indicator call this
1) create baseviewcontroller
class baseviewcontroller: uiviewcontroller { var activityindicator: uiactivityindicatorview = uiactivityindicatorview() override func viewdidload() { super.viewdidload() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func activityindicatorbegin() { activityindicator = uiactivityindicatorview(frame: cgrectmake(0,0,50,50)) activityindicator.center = self.view.center activityindicator.hideswhenstopped = true activityindicator.activityindicatorviewstyle = uiactivityindicatorviewstyle.gray view.addsubview(activityindicator) activityindicator.startanimating() disableuserinteraction() greyview = uiview() greyview.frame = cgrectmake(0, 0, self.view.bounds.width, self.view.bounds.height) greyview.backgroundcolor = uicolor.blackcolor() greyview.alpha = 0.5 self.view.addsubview(greyview) } func activityindicatorend() { self.activityindicator.stopanimating() enableuserinteraction() self.greyview.removefromsuperview() } }
2) assign baseviewcontroller
parent of viewcontroller
this
class viewcontroller1: baseviewcontroller { override func viewdidload() { super.viewdidload() //now if want add activityindicator call self.activityindicatorbegin() //or if want remove activityindicator self.activityindicatorend() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
Comments
Post a Comment