Reputation: 11
I am using ABPersonViewController and adding a label on the "Info" view. The thing is: when I click the "Edit" button, since the : personController.allowsEditing = YES; my view goes to the "edit view" and my Label is still there (not as I planed ) I am trying to figure out if I can be "notify" when the user pressed the "Edit" button , so I can remove my label from the current view before it goes to the "Edity View"
The only option I am thinking about is to disable editing in ABPersonViewController and to create an "Edit" button of my own, then I will have to try and implement the same behavior of the Addressbook "Edit" button ...
Is there an option for a callback , when the "Edit" button is pressed ? and still keep the same behavior of the ABPersonViewController?
or maybe there is a way to know on which view I am in (tag or somthing ... ?) in the ABPersonViewController so I can remove the label when I am not on the "Info" view
Thanks
Itay
Upvotes: 1
Views: 3228
Reputation: 21
Just figured out a slightly hacky looking way to get notified when edit is pressed.
create a subclass of ABPersonViewController
and attach your own custom action to the edit button: In the initialization for your view controller:
-(void)viewDidAppear:(BOOL)animated{
[self.navigationItem.rightBarButtonItem setTarget:self];
[self.navigationItem.rightBarButtonItem setAction:@selector(editPressed)];
}
I couldn't find a better reference to the edit button than: self.navigationItem.rightBarButtonItem
then create your editPressed
action:
-(void)editPressed{
[super setEditing:!super.editing];
if(self.editing){
NSLog(@"Editing");
//Insert code to put your custom view in edit mode
}else{
NSLog(@"Not editing");
//Insert code to take your custom view out of edit mode
}
}
It's important to call [super setEditing:!super.editing]
first as this makes the UIPersonViewController
go in and out of edit mode as appropriate (Defining your custom action overwrites the default action). It also properly updates the the 'editing
' property of your view controller so that 'self.editing
' gives the correct value.
Upvotes: 2
Reputation: 51
Alternatively you can subclass and override setEditing:animated
. This setter is invoked for both Edit
and Done
but not for Cancel
and a callback is still required. The example below hides the toolbar when editing the record and restores it when done.
// Override setter to show/hide toolbar
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
self.navigationController.toolbarHidden = editing;
if (editing) {
[self.navigationItem.leftBarButtonItem setTarget:self];
[self.navigationItem.leftBarButtonItem setAction:@selector(cancel)];
}
}
// Cancel button callback (does not invoke setEditing:animated)
- (void)cancel {
[self setEditing:NO animated:YES];
}
Upvotes: 5