jsttn
jsttn

Reputation: 1505

Where can I initiate the array?

I have a button which performs an action when pressed. I want to add a value to an array. The value is determined after the button is pressed. However, when I initialize the array in the button action, the array resets every time, clearing the array. I want to keep the array values. How can I keep the array values? Should I initialize the array somewhere else? If so, where can I?

Upvotes: 0

Views: 104

Answers (2)

Legolas
Legolas

Reputation: 12325

Your statement -

"However, when I initialize the array in the button action, 
 the array resets every time, clearing the array"

Do not initialize the array in the button action. Initialize it outside as a property of your class or make it global.

In your action for your button, just add the item to your array as [self.myArray addObject:myObject]

Upvotes: 2

JustSid
JustSid

Reputation: 25318

Before creating a new array, check if there isn't already one.

if(!array)
    array = [[NSMutableArray alloc] init];

// ...

[array addObject:myValueToAdd];

Upvotes: 3

Related Questions