我有一個'創建新事件'頁面,用戶可以點擊UINavigationController上的'Back'或'Done'來創建一個新事件。之後都會導致相同的頁面。如何判斷用戶是否按下或完成?
如果我的用戶按下「完成」,保存在控制器中的當前對象將通過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仍然被調用。有人知道爲什麼
====== 這是如何設置新的。
- (void)initWithEvent:(id)sender event:(Event *)ev {
isNew = !ev;
if (isNew) {
event = [[DataStore singletonInstance] createEvent];
} else {
event = ev;
}
}
您是否已將完成和返回按鈕的單獨回調函數關聯? – Saurabh