-->

how to check whether organization name is added be

2020-07-24 04:23发布

问题:

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

回答1:

There is no documented way to do this using ABNewPersonViewController, because when the delegate method is called, the person is already saved by the ABNewPersonViewController. Formerly you should made your own "insert controller" and use it.

But I tried this workaround and obtained this behavior (on iOS 6):

in the delegate method

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person

you receive a ref to the new person created. You can then access to the person saved and check if the company has been compiled. If not, simply you don't call

[self dismissModalViewControllerAnimated:YES];

The ABNewPersonViewController will stay in position, and you can show an alert to the user asking him to compile the company. The fields will remain compiled and ABNewPersonViewController "linked" to the new user created.

Then, the user can:

  • compile the company field --> a tap on Save will update the saved person and give your delegate new data, so you can dismiss the viewcontroller correctly
  • tap cancel --> you receive the cancel, and the person is deleted by the ABNewPersonViewController itself

But...because I can't find this behavior documented in any place, I'm not sure if

  • it will work on other versions
  • it will "survive" to the review process (formerly you are not using private API, but not sure if "not closing" the viewcontroller when the user taps save is not good for the UI Guidelines)

Regards Fabio