Database Reference
In-Depth Information
Next we need to add our delegate method for the EntryCollectionViewCellDelegate .
#pragma mark - EntryCollectionViewCellDelegate Methods
-(void)entryCollectionViewCell:(EntryCollectionViewCell *)cell longPressedForEntry:(NSString *)entry
{
_selectedEntry = entry;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete Entry"
message:[NSString stringWithFormat:@"Are you sure you want to delete the entry for %@",entry]
delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];
[alert setTag:1];
[alert show];
}
On the first line we assign the entry that was passed to us to our _ selectedEntry property. Then we
instantiate an alert view that asks the user if they are sure they want to delete that entry. We set the
tag on the alert to 1 because we will have other alerts later to deal with so we need to know which
one this is. Finally we call show.
Because we are prompting the user with an alert and we want to know what they chose, we need
to subscribe to the UIAlertViewDelegate as well. Select your FriendsCollectionViewController.h
file and add UIAlertViewDelegate to the list of protocols we are subscribing to. The interface should
now look like this:
@interface FriendsCollectionViewController : UICollectionViewController <UICollectionViewDataSource,
UICollectionViewDelegate, EntryCollectionViewCellDelegate, UIAlertViewDelegate>
Now let's add the UIAlertViewDelegate method alertView:clickedButtonAtIndex: .
#pragma mark - UIAlertViewDelegate Methods
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == 1){
if(buttonIndex == alertView.firstOtherButtonIndex)
[self deleteEntry:_selectedEntry];
_selectedEntry = nil;
}
}
We start off by checking to see whether the tag for the alertView is in fact 1. If it is, we check to
see whether the buttonIndex that was passed matched the index of the first other button index.
If it is, which means they tapped on “Yes”, then we call a new instance method deleteEntry: and
pass it the _selectedEntry . Then we set _ selectedEntry to nil. We haven't created the deleteEntry:
method yet so let's create it now:
#pragma mark - Data Methods
-(void)deleteEntry:(NSString *)entry {
[_entries removeObject:entry];
[self.collectionView reloadData];
}
 
Search WWH ::




Custom Search