I have a ‘Create new event’ page where the user can click ‘Back’ on the UINavigationController or ‘Done’ to create a new event. Both leads to the same page afterwards.
If my user presses “Done” the current object kept at the controller is saved via saveChanges. However, if the user has decided to abandon this even he/she is editing, I need to clean up static files generated by this user during this lifetime of this event.
My only problem is that there is no way for me to differentiate a user from pressing ‘Done’ or ‘Cancel’.
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// If user has left this page by either pressing 'Done' or 'Cancel'
if (![[self.navigationController viewControllers] containsObject:self]) {
// If this event is NOT saved aka 'Cancel' is pressed.
if (![event isUpdated]) {
// It is important that we only delete videos associated when isNew.
if (isNew) {
NSURL *url = [self getMovieUrl:event.uuid];
[self deleteMovieByUrlIfExists:url];
}
self.event = nil;
}
}
}
- (IBAction)done:(id)sender {
[self.view endEditing:YES];
if (isNew) {
event.eventType = eventTypeInput.text;
event.targetId= [NSNumber numberWithInt:[targetIdInput.text intValue]];
event.game = game;
BOOL success = [[DataStore singletonInstance] saveChanges];
if(success) {
NSLog(@"Success: Event %@ saved. hasVideo:%d", event.uuid, [event.hasVideo intValue]);
// TODO [newEvent upload];
// Also upload the video if possible.
}
}
[self.navigationController popViewControllerAnimated:YES];
}
The above code does not work. When the user presses ‘Done’, deleteMovieByUrlIfExists:url is still called. Anyone knows why?
======
This is how isNew is set.
- (void)initWithEvent:(id)sender event:(Event *)ev {
isNew = !ev;
if (isNew) {
event = [[DataStore singletonInstance] createEvent];
} else {
event = ev;
}
}
Apparently,
[event isUpdated]andisNewalways have the same value no matter what action the user takes. Set a flag (aBOOLivar, for example) indone:and check the value of the flag inviewWillDisappear:.