Eric Brotto
Eric Brotto

Reputation: 54261

Delete button in UIViewController which is showing the detail of a cell

I have an iPhone app that has a tableview which contains cells that when touched show a detail of that object. I would like to add a Delete button to the bottom of the detail view. When the user presses it the object which is represented by the cell should be removed and the app should return to the TableView.

In terms of best practices, which is the ideal way to accomplish this?

Upvotes: 1

Views: 251

Answers (2)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

There are few ways in which you can signal the deletion. One of them is delegates. You can define your delegate like this,

@protocol DetailViewControllerDelegate
- (void)shouldDeleteDetailObject:(id)object
@end

And then your table view controller subclass adopt the protocol and implement the method like,

- (void)shouldDeleteDetailObject:(id)object {
    [self.objectsArray removeObject:object];
    [self.navigationController popViewControllerAnimated:YES];
}

And then you message [self.tableView reloadData]; in viewWillAppear: as sandy has indicated.

Your button action will be implemented as,

- (IBAction)deleteObject {
    if ( self.delegate && [self.delegate respondsToSelector:@selector(shouldDeleteDetailObject:)] ) {
        [self.delegate shouldDeleteDetailObject:self.detailObject];
    }
}

And delegate should be an assigned property.

You can also look at notifications but this is a better route for this situation.

Upvotes: 2

sandy
sandy

Reputation: 1072

I think there is nothing serious about this, if you successfully delete the particular details after that on backing on previous view (tableview) you just use this

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];  
} 

Upvotes: 0

Related Questions