我有一个“创建新事件”页面,用户可以在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。谁知道为什么?
====== 这就是新的设定方式。
- (void)initWithEvent:(id)sender event:(Event *)ev {
isNew = !ev;
if (isNew) {
event = [[DataStore singletonInstance] createEvent];
} else {
event = ev;
}
}
答案 0 :(得分:0)
显然,无论用户采取什么操作,[event isUpdated]
和isNew
始终具有相同的值。在BOOL
中设置一个标记(例如done:
ivar)并检查viewWillDisappear:
中标记的值。
答案 1 :(得分:0)
你应该为你的视图控制器创建一个委托,委托用于根据动作将数据传输到其他视图控制器,我强烈建议你阅读更多代理,但这里有一个简单的说明
@protocol viewControllerDelegate <NSObject>
@required
//define the delegate methods
-(void)backButtonPressed;
-(void)doneButtonPressed:(id)sender
@end
@interface ViewContorller : xxx
{
id <viewControllerDelegate> delegate;
}
现在在action方法中,在完成数据处理后,你就编写了这行代码
- (IBAction)done:(id)sender {
[delegate backButtonPressed:sender]
}
现在所有实现此委托的视图控制器都会收到通知,你应该创建两个委托方法,一个用于后退按钮,另一个用于完成按钮
//in the parent view controller .h file add the delegate to the definition
@interface MyViewController: UIViewController <viewControllerDelegate>
//in the .m file implement the delegate methods
-(void)backButtonPressed:(id)sender
{
//this method will get called when the back button is pressed
}
-(void)doneButtonPressed:(id)sender
{
//this method will get called when the done button is pressed
}