I am working on a AddressBook project, One of my requirement is While adding new Contacts manually using my application it should check whether "Organization field" value is entered by user/not.
I have Add(+) button on my Navigation Bar with d following code snippet:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:@selector(add:)];
self.navigationItem.rightBarButtonItem = addButton;
A present modal view appears on clicking this add button, by native Address Book;
-(void)add:(id)sender
{
ABNewPersonViewController *view = [[ABNewPersonViewController alloc] init];
view.newPersonViewDelegate = self;
UINavigationController *newNavigationController = [[UINavigationController alloc] initWithRootViewController:view];
[self presentModalViewController:newNavigationController animated:YES];
}
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person
{
newPersonView.displayedPerson = person;
[self dismissModalViewControllerAnimated:YES];
[table reloadData];
}
Before contact is saved to my address Book i want to check if the user has added "Organization field" or not. In case of blank/nil i want to show a Alert Box asking for filling Organization value. It is mandatory, once user provides Organization value then contact would be saved to AddressBook.
EDIT: As suggested below by Fabio, i updated my codes..
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person{
NSString *company = [NSString stringWithFormat: @"%@", ABRecordCopyValue(person, kABPersonOrganizationProperty)];
if ([company isEqualToString: @"(null)"]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Value Required!" message:@"Please provide some value for ORGANIZATION Field..." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
else
{
newPersonView.displayedPerson = person;
[self dismissModalViewControllerAnimated:YES];
}
}
With this i am able to show an Alert to the user to provide field value. It is also updating the subsequent record created. But, as the MODAL VIEW is Dismissed, the Detailed view(Info screen of Native App) shows no information about the contact.
Also, CANCEL Button doesn't works in its regular way.. i can't go back to app, as it repeatedly ask to provide field value, even if i provide and press cancel.
Can any one guide me!
Thanks & Regards