Xavi Valero
Xavi Valero

Reputation: 2057

ABNewPersonViewController delegate method doesn't get called when the button action is triggered

I am using ABNewPersonViewController. I am saving the Done button(rightBarButtonItem of ABNewPersonViewController) to another button, so that the delegate method

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

doesn't get called until done button is pressed. My viewDidLoad method is as

- (void)viewDidLoad {
[super viewDidLoad];


self.defaultRightBarButtonItem = self.navigationItem.rightBarButtonItem;

UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc] 
                            initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
                            target:self 
                            action:@selector(actionSave:)];
self.navigationItem.rightBarButtonItem = saveBtn;
[saveBtn release];
}

On the button click in another view I trigger the done button action

[self.defaultRightBarButtonItem.target 
 performSelector:self.defaultRightBarButtonItem.action
 withObject:self.defaultRightBarButtonItem.target];

Sometimes the method gets called and sometimes doesn't work. If I just edit the email address field or phone number field of the contact and try to save that, the method gets called. But if I try to edit the address fields and then save, the method doesn't get called. What could be the reason for this.

Edit: Found out that the delegate method doesn't get triggered when a new view is loaded. And this happens(new view loaded) only when fields like Country, Ringtone gets edited. Thats when the delegate method is not triggered. In all other cases, the delegate method gets triggered. Now any suggestions?

Upvotes: 2

Views: 1104

Answers (2)

danh
danh

Reputation: 62676

My 2 cents: Nothing in the question's code indicates an error. But why such a round-about call the save method (performing the save button's action on the save button's target). How about:

[self saveAction:nil];

Upvotes: 0

MrTJ
MrTJ

Reputation: 13202

performSelector is equivalent of calling the method of the object to whom it is sent. If the execution enters to the "button click in another view" handler and executes the

[self.defaultRightBarButtonItem.target 
 performSelector:self.defaultRightBarButtonItem.action
 withObject:self.defaultRightBarButtonItem.target];

code, but in turn it never steps into the selector defined in action, only the following cases are possible:

  • self.defaultRightBarButtonItem or self.defaultRightBarButtonItem.target is nil
  • self.defaultRightBarButtonItem.action is nil

Instead of performSelector try to invoke directly the actionSave: method and log all fields above.

Upvotes: 2

Related Questions