-1

i want to create new view every time on click of button. like when i first press button it will execute this code which i tried

 let userResizableView1 = ZDStickerView()

so now when i click next time on this button it should be create new view again with name userResizableView2 so how can i do this?

2 Answers2

0

While you can't create a new name for the variable per se, what you could do is create a new view each time that has a new identifier.

So you could do something like this:

var views: [ZDStickerView] = [ZDStickerView]()

func buttonPressed(sender: UIButton){
    let view = ZDStickerView()
    view.accessibilityIdentifier = "userResizableView" + String(views.count + 1)
    views.append(view)
}

This way you find this specific view again.

For reference, it might be easier if you are willing to use the .tag property which would look more like:

view.tag = views.count + 1

However, you could have problems with this if you were to alter the array later on since the tags would not be in order. I don't know how you are applying this, but just be wary.

Benjamin Lowry
  • 3,730
  • 1
  • 23
  • 27
0

The scenario you have explained above in your question is not possible because you are requiring dynamic name of a variable which is not supported by iOS.
But the logic can be developed for your requirement as:
1) You can create an array, globally to store your view.
2) When you click on a button then add a view in the globally declared array.
3) When you require your view, you can access the same by passing an relavent index.
for example,
you click your button for the first time, add a view in the array.
Now your array contain 1 object.
When you click your button 2nd time, your array contain 2 objects, and so on...
Now,
When you require your 2nd view then you can access

 [<"YourArrayName"> objectAtIndex:2];
Er. Vihar
  • 1,495
  • 1
  • 15
  • 29