2

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?

Xavi Valero
  • 2,047
  • 7
  • 42
  • 80
  • You say that you call the action of your button from another view. Are you sure that defaultRightBarButtonItem is not released when you try to call it? – MrTJ Mar 21 '12 at 10:06
  • Sometimes, the method gets executed, sometimes it isn't. Would that be release problems? – Xavi Valero Mar 21 '12 at 10:09
  • 1
    It might be easily some concurrency situation, especially if your object is autoreleased. Put a breakpoint in that line and try to catch a case when it will not call the selector. Or you can log the value of defaultRightBarButtonItem pointer before calling that line. – MrTJ Mar 21 '12 at 10:11
  • Could you please provide the code to NSLog defaultRightBarButtonItem. thanks – Xavi Valero Mar 21 '12 at 10:13
  • Well I don't know the type of your class but `NSLog(@"%@", self.defaultRightBarButtonItem)` should do the work in most of the cases. If `- (NSString *)description` is not overridden by your class it will print the value of the pointer. – MrTJ Mar 21 '12 at 10:20
  • I tried logging ` NSLog(@"%@", self.navigationItem.rightBarButtonItem);` and ` NSLog(@"%@", self.doneButton.target);` and I got ` ` in the log. – Xavi Valero Mar 21 '12 at 10:53
  • That might be even valid since the pointers (the hex numbers) are not null. Try to see when your selectors are _not_ called if the value of the pointers were != 0 as well. – MrTJ Mar 21 '12 at 11:26
  • Did that. Everything working fine except the method being triggered. – Xavi Valero Mar 21 '12 at 11:36
  • So the code steps into the "button click in another view" and steps over the performSelector call without invoking defaultRightBarButtonItem.action? – MrTJ Mar 21 '12 at 13:04
  • Yes right. It happens when I do edits like change address etc. But if I just edit Phone numbers or email, or even if I don't make any changes and save then the method is triggered. – Xavi Valero Mar 21 '12 at 13:35

2 Answers2

2

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.

MrTJ
  • 13,064
  • 4
  • 41
  • 63
0

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];
danh
  • 62,181
  • 10
  • 95
  • 136