0
votes

I am currently using the paradigm I'm about to describe below in my application and it works, so It's not a big problem; however, I would like to know if I'm breaking some kind of rule with this pattern.

I am using ARC.

I have an application which presents a modal viewController. It has a table view on it.

I then have an "add" button which allows me to enter some data into the table view.

Pressing add, pops up a little dialog box created from a UIView with many textFields. - Each textField is an instance variable.

I'm alloc initing the textField instance variables when my add method gets called.

My question is: Is it OK to call this add method on my viewController which instantiates the instance variables and uses them, and then later, call the same method again which would be calling alloc init on instance variables that were already allocd and inited the last time the method was called.

Thus calling alloc init on the same ivars without niling them first - i'm also using ARC?

Something like this pseudocode:

.h

@property UITextField *textField;

.m

@synthesize textField;



(void) someMethod {

textField = [[UITextField alloc] init] 
[textField doSomethingElse]

}

LATER

Some method is called over and over again.

Should I instead alloc init all the textfields, and just add and remove them from my UIView in the add method?

Thanks, Regards, John

2
If you are reusing the text fields it's pretty unnecessary to be creating them over and over each time that method is called don't you think? - Peter Warbo
Sure so how can I avoid creating them over and over, just alloc init in viewDidLoad perhaps? - Woodstock

2 Answers

1
votes

By definition you can't call alloc on existing instances. What you're doing is causing the old instances to be destroyed and creating new instances to replace them. There is nothing necessarily wrong with this, but it could be quite wasteful and, depending on how hard it is to create those instances, time consuming.

1
votes

When your add controller is dismissed ARC should take care of deallocating all the instance variables. So there is no harm in recreating them when you need them again.