我有一个“创建新事件”页面,用户可以在其中单击 UINavigationController 上的“返回”或“完成”来创建新事件。之后两者都指向同一页面。
如果我的用户按下“完成”,则保存在控制器中的当前对象将通过 saveChanges 保存。但是,如果用户决定放弃这个,即使他/她正在编辑,我需要清理这个用户在这个事件的生命周期内生成的静态文件。
我唯一的问题是我无法区分用户按“完成”或“取消”。
- (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];
}
上面的代码不起作用。当用户按下“完成”时,仍会调用 deleteMovieByUrlIfExists:url。有谁知道为什么?
====== isNew 就是这样设置的。
- (void)initWithEvent:(id)sender event:(Event *)ev {
isNew = !ev;
if (isNew) {
event = [[DataStore singletonInstance] createEvent];
} else {
event = ev;
}
}