views:

15

answers:

0

Hi

I am trying to copy an object using the NSCopying protocol. I load the new controller on iphone and create a copy of a object. All the changes from TextFields are done on the copy. If the user wants to save I guess I have to somehow replace the original one with the copy.. But it does not take over the values...

This is the part where I load the new controller. (book) is the object.

childController.title = book.name;
    childController.book = book;

    TableNavAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    UINavigationController *navController = [delegate navController];
    [navController pushViewController:childController animated:YES];
    [childController release];

In the new controller I create a copy of book during viewDidLoad()

self.bookCopy = [book copy];

And this is where I save and pop the view. I have added an NSLog output to check if the value has been copied back which did not. An NSLOG to bookCopy.name works fine. The changes are in the copy.

- (void) save:(id)sender
{
    if ( textFieldBeingEdited != nil )
    {
        NSNumber *nr = [[NSNumber alloc] initWithInt:textFieldBeingEdited.tag];

        switch ( [nr intValue] ) {
            case kPhonebookName:
                bookCopy.name = textFieldBeingEdited.text;
                break;
            case kPhonebookPhone:
                bookCopy.phone = textFieldBeingEdited.text;
                break;
            case kPhonebookMail:
                bookCopy.mail = textFieldBeingEdited.text;
                break;
            case kVisibilityField:
                bookCopy.viz = textFieldBeingEdited.text;
                break;
            case kCurrentField:
                bookCopy.current = textFieldBeingEdited.text;
                break;
            default:
                break;
        }
    }

    self.book = bookCopy;
    NSLog(@"%@",book.name);
    // Pop childview and reload
    TableNavAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    UINavigationController *controller = [delegate navController];

    [controller popViewControllerAnimated:YES];

    NSArray *allControllers = controller.viewControllers;
    UITableViewController *parent = [allControllers lastObject];

[parent.tableView reloadData]; }