1

我有一个“创建新事件”页面,用户可以在其中单击 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;
    }
}
4

2 回答 2

0

显然,无论用户采取什么行动[event isUpdated],总是具有相同的价值。isNew在 中设置一个标志(BOOL例如,一个 ivar)done:并检查标志的值viewWillDisappear:

于 2012-06-10T10:24:29.737 回答
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
 }
于 2012-06-10T13:54:18.493 回答