Question

I am new to x-code and I was wondering: Is it possible to make check marks retain though you go through a modal segue?

I will check some boxes on my list:

enter image description here

But when I press done and then come back to the screen with a modal segue it appears as such:

enter image description here

Is it possible to make these checkmarks stay though I change views modally?

I have this code to create the checkmarks:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.toDoItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ListPrototypeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NewItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;
    if (toDoItem.completed) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NewItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
    tappedItem.completed = !tappedItem.completed;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
Was it helpful?

Solution

When you pop or dismiss a view controller, that view controller is gone. However, you have a few options to remember the state that view controller was in. The easiest way is probably to store a global variable, maybe an NSArray, to remember the checked items. Then when you load this view controller, you could "check" any items that exist within that NSArray.

Note that this method will only work for the lifetime that the app is open. If they closed the app, it would be gone. If you wanted to maintain the "checked" items for the next time they opened the app, you would need to store it in the NSUserDefaults - data in there is available until the app is deleted from the phone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top