Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Kieran Robinson
Kieran Robinson
9,411 Points

Moving swipe to delete into detail view

In my app, i have a tableview that currently uses swipe to delete. I implemented this by following the section within the diary app with Ash Furrow. however i wish to have the delete function as a button at the bottom of the detail view. Any ideas?

Ash Furrow
Ash Furrow
Treehouse Guest Teacher

Hmm. Not quite sure what you mean. Can you elaborate?

Kieran Robinson
Kieran Robinson
9,411 Points

I will use the diary app as an example. I have the list of diary entries on 'entryListViewController' and to remove one i currently swipe to delete. However on the 'edit' page of the diary app I would like a button at the bottom of the page to delete that entry?

Ash Furrow
Ash Furrow
Treehouse Guest Teacher

Ah, that's a bit trickier. Adding the button is easy, but having it do something is the hard part. You'd need to set up a callback in the delegate protocol to delete the entry, and then immediately dismiss the edit view controller. Does that make sense?

Kieran Robinson
Kieran Robinson
9,411 Points

Im not quite sure what you mean, which methods would i use or remove, using the diary app as an example?

Ash Furrow
Ash Furrow
Treehouse Guest Teacher

Ah, right, sorry. Forgot we didn't use the delegate pattern in this case. Your code to delete would look something like:

-(void)deleteEntry {
    /*delete the entry*/
    /*save the context*/
    /*dismiss the view controller*/
}

Shouldn't be too hard. Let me know if you run into any issues.

Kieran Robinson
Kieran Robinson
9,411 Points

Thank you! i will have a look now, is this still within 'entryListViewController' or the 'EntryViewController' where the button would be?

Ash Furrow
Ash Furrow
Treehouse Guest Teacher

Yup – in the entry view controller.

Kieran Robinson
Kieran Robinson
9,411 Points

I will try it now! one last thing, i've been trying to teach myself how to add a search bar and display controller, but most of the tutorials are for older iOS versions, has anything changed in iOS 7 that would deem these tutorials useless?

Ash Furrow
Ash Furrow
Treehouse Guest Teacher

No, not really. iOS 8 changes some things, but the concepts you learn will still be relevant.

Kieran Robinson
Kieran Robinson
9,411 Points

Hi Ash Furrow

-(void)deleteEntry {

CoreDataStack *coreDataStack = [CoreDataStack defaultStack]; DiaryEntry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];

//delete entry
[[coreDataStack managedObjectContext] deleteObject:entry];

//save context

[coreDataStack saveContext];

//dismiss view controller
[self dismissSelf];

}

also, what methods do i take out of the entryListViewController?

Kieran Robinson
Kieran Robinson
9,411 Points

not sure why it formatted my comment that way, it won't change! hope you can still understand the method?

Ash Furrow
Ash Furrow
Treehouse Guest Teacher

Interesting. That should be working. What's happening instead? Can you post a project that's not working so I can take a closer look?

Kieran Robinson
Kieran Robinson
9,411 Points

Ash Furrow its working now! Thank you for your time

1 Answer

Kieran Robinson
Kieran Robinson
9,411 Points

Ash's answer solved this question!