Using the same View Controller to add, display and edit

I'm working on an iOS application that uses a very common Core Data based tabular view to display items, and when selected, it shows a more detailed view, similar to the Contacts application. The detail view is a generated programmatically grouped table with a custom (defined by nib) view for the header, which has an image and a name. Some of the cells in the table are user cells that have a label name and a text field value. In edit mode, the editable table cells (and the name in the header) have .clearButtonMode set to UITextFieldViewModeAlways to indicate that they are editable.

I am currently using the same view controller to display detailed information, edit information, and add a new record to the original list.

When a new item is added, the view controller is created using a special init reload, which sets a flag in the view controller to indicate that it is adding a record. This allows it to start in edit mode, and if the edit mode remains, the model view disappears. The right menu button is the usual Edit / Done, and the left is the cancel button. When an existing item is edited, the left button (normal back button) is replaced by the cancel button.

I'm starting to wonder if there is a way to control one controller in three different modes. There are several issues that I'm not sure how to handle.

1) How do I know if editing mode is left by clicking Finish? Is there any action for this? If the cancellation is deleted, the action either cancels itself (add mode) or restores the previous values, exits the editing mode. I suppose I could put a check in my setEditing override to handle it, but there seems to be a better way.

2) When edit mode is entered and I set editable text fields to UITextFieldViewModeAlways, is there any way to animate the appearance of the “X” buttons so that they disappear with the edit indicators on regular cells?

3--1 - ? , , .

Jorj

+5
2

3--1 . : xib, , . , if (self.editing) ..., , xib.

, BOOL, .

@property (nonatomic) BOOL adding;

1) editButtonItem setEditing: . , , Done . editButtonItem Edit, Done Cancel . . .

2) UITableView reloadSections: withRowAnimation. .

- (void)edit:(id)sender 
{
    self.editing = YES;
}
- (void)done:(id)sender 
{
    // data validation here
    if (everythingChecksOut)
    {
      //save here
    } else {
      return; //something didn't validate
    }

    //if control reaches here all is good
    //let the delegate know what happened...
    if (self.adding) {
      [self.delegate didFinishAddingWithData:self.yourData];
    } else {
      [self.delegate didFinishEditingWithData:self.yourData];
    }

    self.adding = NO;
    self.editing = NO;
}
- (void)cancel:(id)sender
{
    [self.view endEditing:YES]; //in theory, forces the view that is editing to resign first responder
    //in practise I find it doesn't work with the YES parameter and I have to use my own flag

    // roll back any changes here

    self.editing = NO;

    if (self.adding) //let the delegate know we cancelled the add...
    {
        [self.delegate didCancelAdd];
    }
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    //set your nav bar title
    [self.tableview.editing = editing]; //you may or may not require this
    [self.tableview reloadSections... withRowAnimation:yourChoice];

    if (editing)
    {
        //install your Done and Cancel buttons
    } else {
        //remove Cancel and put the Edit button back
    }
}

viewDidLoad...

- (void)viewDidLoad
{
    [super viewDidLoad];

    //whatever else you do

    if (self.adding)
    {
        self.editing = YES;
    }
}
+3

, , , , , ...

, UITableViewController, if . UITableViewControllers, ( , ), . , .

detailViewController , , -, .

, , , detailView, "", "" "". viewController .

, ...

+1

All Articles