title: Is there a way to automatically tie a UITextField to a variable in my data model?
body: <p>I'm building up a view controller where I'm adding a bunch of <code>UITextField</code>s to my view programmatically.  I want to pre-populate the textfields with some text that I'm loading from CoreData, but allow the user to go in and change that text if they want.  I then need to go back and save that new text back to CoreData again, as well as perform calculations using the new value.  The saving and loading itself isn't a problem, but I'm looking for a good way to track what the user has changed.  </p>
  
  <p>In the past, I've done this with the <code>.tag</code> property of the <code>UITextField</code> and the <code>textFieldDidEndEditing:</code> delegate method.  The problem is I'm going to have dozens of textfields in this view - enough so that I can't really remember what <code>.tag</code> belongs to what variable in my model.  Having to go back to look up what tag I assigned when I created it is going to be a pain, and the whole thing feels really error prone.  Additionally, I'm going to be creating the textfields in multiple different loops, so getting a unique tag for each textfield when I create it will be difficult (maybe impossible?).</p>
  
  <p>What I'm looking for is a way to "tie" the UITextField to a variable in my model when I create it and just know from that point on, whenever the user updates that textfield, the value I specify in my model will be instantly updated to be used in future calculations, and saving back to CoreData when the user leaves the screen.  Is there a way to do this?</p>
  
  <p>EDIT 1: Another thing to note is that my model itself is kinda complex.  I have several different custom <code>NSObject</code> subclasses that are all holding different pieces of my data.  Some of the objects have properties that are themselves other custom objects.  I also have dictionaries where the values will be instances of some of the custom objects.  </p>
  
  <p>As an example, I might have these objects:</p>
  
  <ul>
  <li><code>MySmallObject1</code>, which has properties <code>name</code>,<code>size</code>, and <code>value</code> (all <code>NSStrings</code>) </li>
  <li><code>MySmallObject2</code>, which has properties <code>date</code> and <code>cost</code> (an <code>NSDate</code> and an <code>NSNumber</code>)</li>
  <li><code>MyBigObject</code>, which has properties <code>box1</code> and <code>box2</code> (which are <code>MySmallObject1</code> and <code>MySmallObject2</code> instances, respectively)</li>
  <li><code>theDict</code>, which is an <code>NSDictionary</code> instance that has <code>MyBigObject</code>s as values</li>
  </ul>
  
  <p>So when I am building my textfields, I might actually be going down a tree that would look something like: </p>
  
  <pre><code>for (NSString *key in [theDict allKeys])
  {
      UITextField *txt = [[UITextField alloc] initWithFrame:...];
      txt.text = [[(MyBigObject *)[theDict objectForKey:key] box1] name];
      [self.view addSubview:txt];
  }
  </code></pre>
  
  <p>In this case, I need to know when the user changes the text in any of the <code>txt</code> and update the appropriate value through the tree.</p>
  
  <p>EDIT 2: While trying to implement @Till's suggestion in the comments, I started reading a <a href="http://pw1.netcom.com/~tjensen/ptr/ch1x.htm" rel="nofollow">tutorial on pointers in C</a>.  It seems like this is the way I need to go, but I'm struggling to get the <code>&amp;</code> and <code>*</code> from the C world to play nicely with the NSObject subclasses I mention in my last EDIT from the Objective-C world.  To get @Till's suggestion to work, I think I need to somehow store the memory location of my value up that complicated object tree.  So my code block from the last EDIT would become:</p>
  
  <pre><code>.h
  @property (nonatomic, strong) NSMutableDictionary *tagDict;
  
  .m
  for (NSString *key in [theDict allKeys])
  {
      UITextField *txt = [[UITextField alloc] initWithFrame:...];
      txt.text = [[(MyBigObject *)[theDict objectForKey:key] box1] name];
      txt.tag = globalCounter;
      [tagDict addObject:[[(MyBigObject *)[theDict objectForKey:key] box1] name] forKey:[NSString stringWithFormat:@"%d",globalCounter]];
      globalCounter ++;
      [self.view addSubview:txt];
  }
  </code></pre>
  
  <p>Where <code>globalCounter</code> is an <code>int</code> that I'm incrementing every time I put something into <code>tagDict</code> so I can keep it unique.  I guess in this case I could just simply be using an <code>NSArray</code> and it would work just as well, and probably look a little cleaner.  My plan is to use <code>tagDict</code> in my <code>testFieldDidEndEditing</code> like:</p>
  
  <pre><code>- (void) textFieldDidEndEditing:(UITextField *)textField
  {
      *[tagDict objectForKey:[NSString stringWithFormat:@"%d",textField.tag]] = textField.txt;
  }
  </code></pre>
  
  <p>This throws an error for <code>Assigning to 'id' from incompatible type 'NSString *'</code>.  I'm not really sure what that means.  Is there a way I can use a "dereferencing operator" to change the value of the item I am pointing to in my dictionary?</p>
  
  <p>throws an error <code>Address expression must be an lvalue or a function designator</code>.  I'm still a little fuzzy on how this pointer stuff would work in the Objective C world, and I think there's something I'm not fully understanding.</p>