uiimageview - Swift: Remove All from the Subview -
i create multiple objects under same name , adding them subview (variables in german).
wandx = (screenbreite - ((felderanzx - 0) * feldbreite)) wandy = ( screenhoehe - ((felderanzy - 5) * feldbreite)) for(var = 0; < 6; i++){ wand1 = uiimageview(frame: cgrectmake(wandx, wandy, feldbreite, feldbreite)) wand1.image = wand self.addsubview(wand1) wandxarray.insert(wandx, atindex: i) wandyarray.insert(wandy, atindex: i) wandx = wandx + feldbreite }
(creating row of walls)
but if want remove them wand1.removefromsuperview()
removes last object added. possible solution found putting object on top , delete references. many object , many stages problem cpu usage.
edit: using method self.view.subviews.removeall()
is getting me following error:
cannot use mutating member on immutable value: 'subviews' get-only property
wand1 = uiimageview(... rewriting reference on , over, never able remove last item created superview. either going have use array or dictionary:
class class { var array = [uiimageview](); ... func something() { ... for(var = 0; < 6; i++){ let wand1 = uiimageview(); wand1.image = wand array.append(uiimageview(frame: cgrectmake(wandx, wandy, feldbreite, feldbreite))) self.add.subview(wand1)//dunno how works code wandxarray.insert(wandx, atindex: i) wandyarray.insert(wandy, atindex: i) wandx = wandx + feldbreite } ... func removethisimage(index : int) { array[index].removefromsuperview(); }
or can create object references every image create, each unique name
//no longer allowed if want remove subviews view , not concerned removing specifics, call self.subviews.removeall()
self view contains subviews. //
looks have write own extension method handle this:
extension uiview { func clearsubviews() { subview in self.subviews as! [uiview] { subview.removefromsuperview(); } } }
then use it, self.view.clearsubviews();
Comments
Post a Comment