ABNewPersonViewController deletes contact on cancel - iphone

I am using an ABNewPersonViewController to create a person. Everything works fine so far. I do set multivalue properties as well as single values. After tapping "Add" the contact can be found in the adressbook.
But there is a problem when editing this contact. After adding the contact to the adressbook, I save the addressbookID, so that I am able to identfiy if the contact is still saved in the AB.
So If the user wants to export a contact again, I do not create a new ABPersonRecordRef, but using the existing one identified by the id I have saved before:
ABRecordID recordId = [aContact.addressBookRecordId intValue];
ABRecordRef personRecord = nil;
if(recordId != 0) {
personRecord = ABAddressBookGetPersonWithRecordID(addressBook, recordId);
if(personRecord) {
return personRecord;
} else {
personRecord = ABPersonCreate();
}
} else {
personRecord = ABPersonCreate();
}
//set properties etc.
The problem now is, that this just works the first time. If the user displays the person a second time in the ABNewPersonViewController and taps on Cancel, the record will be deleted in the adress book. Although it was previously saved fine.
I tried using an ABUnknownPersonViewController, but the problem is that it seems that the attributes det on the recordRef are not displayed exactly like in the ABNewPersonViewController.
Any suggestions?

Though this thread is old, it might help others. Try to override the cancel event as shown here. It worked for me.
- (void)showNewPersonViewController:(ABRecordRef)person
{
//show new Person view controller
ABNewPersonViewController *vcNewPerson = [[ABNewPersonViewController alloc] init];
vcNewPerson.newPersonViewDelegate = self;
vcNewPerson.displayedPerson = person;
vcNewPerson.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(onNewPersonCancelClick)];
[self.navigationController pushViewController:vcNewPerson animated:YES];
[vcNewPerson release];
}
- (void)onNewPersonCancelClick
{
[self dismissViewControllerAnimated:YES completion:nil];
}

This will be of use to you:
Can ABNewPersonViewController be used to edit existing records?
It covers a tutorial with your exact problem.

Related

UIActivityViewController not dismissing

I'm trying to add a custom UIActivity to a UIActivityController. When I click on the item, it presents the view controller I want it to, but when I finish with that view controller, the original UIActivityViewController is still there. My question is, how and where do I dismiss the activity view controller? This is the code in my custom UIActivity.
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems{
self.activityTitle = #"Text Editor";
self.activityType = #"TextEdit";
self.activityImage = [UIImage imageNamed:#"Icon.png"];
if (activityItems) return YES;
else return NO;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems{
for (NSString *path in activityItems) {
if ([path lastPathComponent]) {
self.file = path;
}
}
}
- (UIViewController *)activityViewController{
ACTextEditController *actec = [[ACTextEditController alloc] initWithFile:_file];
return actec;
}
EDIT
I've tried doing this, and I know it is called because I tried logging something in it and it was called
- (void)activityDidFinish:(BOOL)completed{
if (completed) {
[self.activityViewController dismissViewControllerAnimated:YES completion:nil];
}
}
however, it's still there when the view controller dismisses. Why?
Here's a working example:
http://www.apeth.com/iOSBook/ch26.html#_activity_view
Notice that we end up by calling activityDidFinish:. That is what tears down the original interface. It may be that that is the step you are omitting. If not, just compare what you're doing with what I'm doing, since what I'm doing works (on iPhone).

EKEventEditViewActionDeleted called several times while deleting event

I am trying to save an event to iPhone calendar also to delete when user deletes the event. following is the code I am using for creating and editing an event .
// Upon selecting an event, create an EKEventViewController to display the event.
EKEventEditViewController *editController = [[EKEventEditViewController alloc] init];
editController.event = [eventsList objectAtIndex:indexPath.row];
editController.eventStore = self.eventStore;
editController.editViewDelegate = self;
itsSelectedReminder = indexPath.row;
isReminderDeleted = TRUE;
[editController.navigationBar setTintColor:[UIColor colorWithRed:67/255.0 green:114/255.0 blue:18/255.0 alpha:1]];
[self presentModalViewController:editController animated:YES];
[editController release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Then When the user does any action add, edit or delete I am catching the event using the follwoing code.
- (void)eventEditViewController:(EKEventEditViewController *)controller
didCompleteWithAction:(EKEventEditViewAction)action {
It works perfect for add and edit however when I tried to delete It is calling the method several times so that makes my app crash. Any help is greatly appreciated. Please help as soon as possible.
Thanks in advance
Regards,
Dilip...
Set the editViewDelegate of the controller to nil immediately on completion.
func eventEditViewController(controller: EKEventEditViewController, didCompleteWithAction action: EKEventEditViewAction) {
// prevent additional calls for the same action
controller.editViewDelegate = nil
// whatever else you want to do
dismissViewControllerAnimated(true) {
}
...
}

Contact being removed from AddressBook after being Selected

I'm working on some integration between my application and the iPhone's AddressBook. Here is the flow of my program.
User wants to import a contact
App Presents "ABPeoplePickerNavigationController" to the User.
User selects the contact they want:
Delegate Method is called:
Here is the code:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
self.selectedContact = person;
[self showNewContactViewThroughController:peoplePicker withRecord:person];
NSLog(#"should continue after selecting");
return NO;
}
5: In - (void)showNewContactViewThroughController:withRecord: We create the AbNewPersonViewController.
`ABNewPersonViewController *newController = [[ABNewPersonViewController alloc] init];`
`newController.displayedPerson = person;`
`newController.newPersonViewDelegate = self;`
And then push it.
6: User hits "Cancel" to quit the ABNewPersonViewController view.
7: Check the Contacts app, the contact they selected in step 3 is gone. Poof, gone, deleted, removed.
In an attempt to fix this issue, I save the ABRecordRef (to the instance variable "selectedContact"). Then, in - (void)newPersonViewController:didCompleteWithNewPerson: I have:
if (person) {
//do stuff with the person
else {
///
/// This means they canceled, so we need to save the old person back
///
if (self.selectedContact) {
ABAddressBookRef addressBook = ABAddressBookCreate();
CFErrorRef error = NULL;
ABAddressBookAddRecord(addressBook, self.selectedContact, &error);
if (error != NULL) {
NSLog(#"Error Adding Record: %#", error);
}
ABAddressBookSave(addressBook, &error);
if (error != NULL) {
NSLog(#"AccountDetailTableViewController.newPersonViewController() The old contact was not saved successfully. %#", error);
}
self.selectedContact = nil;
}
}
However, this doesn't seem to do anything. The code is executed, but the "Old Contact", self.selectedContact, is not saved to the AddressBook. So, my contacts are still disappearing. What am I doing wrong? It appears as though if you hit Cancel in the ABNewPersonViewController, it "removes" the data it was given from the Address Book? So the person I give it then dies? Any help would be appreciated!
I solved this problem by NOT using the ABNewPersonViewController, but rather by using the ABPersonViewController after the user selected the person they want to use. The information is no longer being deleted.

TTPostController - Keyboard is not appearing

Hi
I am adding a "post comment section in my iPhone APP"
rest all things are working fine but when I tap on "postController textView" a keyboard is suppose to appear from the bottom but it is not appearing However the cursor is displaying and the text that I am passing using postController.textView.text = #"" is also displaying.
Please suggest the areas to be looked for fixing this bug.
-(void)showCommentView
{
TTPostController *postController = [[TTPostController alloc] init];
// self must implement the TTPostControllerDelegate protocol
postController.delegate = self;
self.popupViewController = postController;
// assuming self to be the current UIViewController
postController.superController = self;
postController.textView.text=#"temporary text";
[postController showInView:self.view animated:YES];
[postController release];
}
above is the code that is giving call to the Three20 PostController
below is the calling method which is unchanged...
-(IBAction)postComment:(id)sender
{
[UserManager instance]authenticateUserAndProceed:self withSelector:#selector(showCommentView)];
}
-(void)showCommentView
{
TTPostController *postController = [[TTPostController alloc] init];
// self must implement the TTPostControllerDelegate protocol
postController.delegate = self;
self.popupViewController = postController;
// assuming self to be the current UIViewController
postController.superController = self;
postController.textView.text=#"temporary text";
[postController showInView:self.view animated:YES];
[postController release];
}
changed method
-(void)authenticateUserAndProceed:(id)parent withSelector:(SEL)selector
{
theParentViewController = parent;
self.theFunctionToCall = selector;
if(userid == nil)
{
GetUserInfoViewController *guivc = [[GetUserInfoViewController alloc] init];
[parent presentModalViewController:guivc animated:YES];
guivc.delegate = self;
[guivc release];
}
else {
//////////////////// below line was replaced///////////
// 2. [theParentViewController performSelector:selector];
// with below code
UIAlertView *alert =[[UIAlertView alloc]initWith Title........
[alert show];
}
}
PROBLEM SUMMARY:
as soon as the user registered, he was not able to the kyboard for the TTPostController
CHANGE SUMMARY:
As soon as the user is registered the call to
[theParentViewController performSelector:selector];
is not sent directly but the call goes to ann alertview which inter calls it.
EFETCS SUMMARY:
the user will see a "you are registered successfully" (kind of) alertview.
tapping OK on which, he will be sent to TTPostController. (this time the keyboard is appearing)
I kept the line# 2 in the AlertViewDelegate method.
I was amazed o see it working just by not calling the line 2 directly.
this worked for me.
I welcome any new and better idea to implement the same

iPhone: Attempt to Switch Views Produces EXC_BAD_ACCESS on the Third Switch Only

I have implemented an app that shows a map with a lot of pins on it.
If you push one pin you get on a second view that shows the data behind the pin.
A button takes you back to the map.
My problem is that by the third touch on a pin the program crashes with a EXC_BAD_ACCESS in this method:
- (void) switchViews {
if(self.details == nil){
Kundendetails *detailAnsicht = [[Kundendetails alloc] initWithNibName:#"ViewList" bundle:nil];
detailAnsicht.rootViewController = self;
self.details = detailAnsicht;
detailAnsicht.map = self.map;
}
if(self.details.view.superview == nil) {
[map.view removeFromSuperview];
[self.view addSubview:details.view];
[details viewDidLoad];
} else {
[details.view removeFromSuperview];
[details release];
[self.view addSubview:map.view];
}
}
How do I isolate which line of code causes the crash? Why would it always crash only on the third touch?
I hope you could help me.
Put NSLog statements in each branch of the ifs. You will almost assuredly see that this statement causes the problem:
[details viewDidLoad];
This is because at some point you execute this:
[details release];
effectively making details inaccessible. By the way you should also almost NEVER call viwewDidLoad directly.