title: iPhone - How to change String in Label properly?
body: <p>I am currently learning to program on iPhone.</p>
  
  <p>I currently have a small test app with a mock up keypad, so I have a few buttons that when pressed should place some text in a label.</p>
  
  <p>I then take whats in the label and use it to open an ABUNKnownPersonControllerView for use there. Just a basic app to get used to a few things in the iPhone.</p>
  
  <p>Where I am having trouble seems to be with working with the string used to display what I want in the label. I get the EXC_BAD_ACCESS error so I've re-read the memory management docs but I cant find anywhere that there should be a memory leak. </p>
  
  <p>So I think my problem is I am incorrectly working with the String. My code is as follows:</p>
  
  <p>//Header file</p>
  
  <pre><code>@interface KeyPadViewController : UIViewController &lt;ABUnknownPersonViewControllerDelegate&gt;{
  IBOutlet UILabel *phoneNumber;
  //NSString *number;
  }
  
  @property (nonatomic, retain) UILabel *phoneNumber;
  //@property (nonatomic, retain) NSString *number;
  
  -(IBAction)addNum1;
  -(IBAction)addNum2;
  -(IBAction)showUnknownPersonViewController;
  </code></pre>
  
  <p>//Implementation file</p>
  
  <pre><code>- (void)viewDidLoad
  {
  [super viewDidLoad];
  self.title = NSLocalizedString(@"Keypad",@"Keypad");
  //self.number = @"";
  self.phoneNumber.text = @"";
  }
  
  
  -(IBAction)addNum1{
  
  //self.number = [self.number stringByAppendingString:@"1"];
  self.phoneNumber.text = [self.phoneNumber.text stringByAppendingString:@"1"];
  
  }
  
  -(IBAction)addNum2{
  
  //self.number = [self.number stringByAppendingString:@"2"];
  self.phoneNumber.text = [self.phoneNumber.text stringByAppendingString:@"2"];
  
  
  }
  </code></pre>
  
  <p>And the showUnknownPerson view in the implementation file:</p>
  
  <pre><code>-(void)showUnknownPersonViewController
  {
      //NSString *contactNum = self.phoneNumber.text;
      ABRecordRef aContact = ABPersonCreate();
      CFErrorRef anError = NULL;
      ABMultiValueRef email = ABMultiValueCreateMutable(kABPersonPhoneProperty);
  
      bool didAdd = ABMultiValueAddValueAndLabel(email, self.phoneNumber.text, kABPersonPhoneMobileLabel, NULL);
  
      if (didAdd == YES)
      {
          ABRecordSetValue(aContact, kABPersonPhoneProperty, email, &amp;anError);
          if (anError == NULL)
          {
              ABUnknownPersonViewController *picker = [[ABUnknownPersonViewController alloc] init];
              picker.unknownPersonViewDelegate = self;
              picker.displayedPerson = aContact;
              picker.allowsAddingToAddressBook = YES;
              picker.allowsActions = YES;
  
  
              [self.navigationController pushViewController:picker animated:YES];
              [picker release];
          }
          else 
          {
              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                              message:@"Could not create unknown user" 
                                                             delegate:nil 
                                                    cancelButtonTitle:@"Cancel"
                                                    otherButtonTitles:nil];
              [alert show];
              [alert release];
          }
      }   
      CFRelease(email);
      CFRelease(aContact);
  }
  </code></pre>
  
  <p>And the dealloc:</p>
  
  <pre><code>- (void)dealloc
  {
  [phoneNumber release]; //@synthesize
  </code></pre>
  
  <p>// [number release]; //@synthesize
      [super dealloc];
      }</p>
  
  <p>I have tried using a Mutable string but have the same error, I've also tried not releasing the UILabel and the NSString and have the same issue.</p>
  
  <p>When my app loads if I press one button and one character appears in the label, if I then use the showUnknowPersonViewController everything works fine, have if I press button 1 and then button 2 so that I know have two characters in the UILabel ("12") and select to use the showUnknownPersonViewcontroller then I get the crash.</p>
  
  <pre><code>*** -[CFString release]: message sent to deallocated instance 0x1226a0
  </code></pre>
  
  <p>However I don't understand where it could be getting deallocated? </p>
  
  <p>This is probably something basic I've missed in the tutorials but I cant figure it out, can anyone see what I might be doing wrong?</p>
  
  <hr>
  
  <p><strong>EDIT:</strong> Just to add if the self.phoneNumber.text (UILabel) returns either empty or one character it works fine however if its 2 or more I get the crash.</p>
  
  <hr>
  
  <p><strong>EDIT 2</strong>: I have removed my NSString from the code completely and I still have the same issue, so it looks like its the UILabel thats causing the issue? See code changed above.</p>