撤消在子视图控制器中进行的所有更改 [英] undo all changes made in the child view controller

查看:246
本文介绍了撤消在子视图控制器中进行的所有更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个实体:作者和书。作者有一个属性authorName和一对多关系书。书有几个属性和关系作者。有一个视图控制器(VCAuthor)来编辑一个Author对象。子视图控制器(VCBook)是编辑作者的书。只有一个managedobjectcontext。在VCBook类中,我将undomanager分组如下:

There are two entities: Author and Book. Author has an attribute authorName and a to-many relationships books. Book has several attributes and a relationship author. There is a view controller (VCAuthor) to edit an Author object. The child view controller(VCBook) is to edit books of the author. There is only one managedobjectcontext. In the VCBook class i group the undomanager as following

-(void)viewDidLoad
{    
    NSUndoManager *anUndoManager = [[NSUndoManager  alloc] init];
    [self.book.managedObjectContext setUndoManager:anUndoManager];
    [anUndoManager release];
    [self.book.managedObjectContext.undoManager beginUndoGrouping];
}

-(void)cancelAction:(id)sender
{
    NSLog(@"%@", self.author.authorName);
    [self.book.managedObjectContext.undoManager endUndoGrouping];
    [self.book.managedObjectContext.undoManager undoNestedGroup];
    self.book.managedObjectContext.undoManager = nil;
    NSLog(@"%@", self.author.authorName);    
    [self dismissModalViewControllerAnimated:YES];  
}

cancelAction链接到VCBook上的取消按钮,

the cancelAction is linked to an cancel button on the VCBook which used to undo all the changes made in the VCBook.

问题在这里:首先,在VCAuthor中,我在一个UITextfiled authorNameTextField从Obama到Big Obama编辑authorName,并保存到MOC由author.authorName = authorNameTextField.text在 - (void)viewWillDisappear:(BOOL)animated {}方法中。然后我进入子视图控制器VCBook编辑作者的书,并单击取消按钮回到VCAuthor。我发现authorName仍然是奥巴马,这意味着已经撤消了authorName的预期更改。 authorName的更改不在undo组中,为什么会发生这种情况? ps。当然我reloadData当我回到VCAuthor。
我只是NSLog的authorName之前和之后的撤销。在撤消之前,authorName是改变的一个大奥巴马,之后撤消它成为奥巴马

Problems is here: First, in the VCAuthor, I edit the authorName in an UITextfiled authorNameTextField from Obama to Big Obama, and save it to the MOC by author.authorName = authorNameTextField.text in the - (void)viewWillDisappear:(BOOL)animated{} method. Then I went into the child view controller VCBook to edit books of the author and click the cancel button to get back to the VCAuthor. I find the authorName still be Obama, that means the expected change of the authorName has been undo. The change of the authorName is not in the undo group at all, and why could this happen? ps. of course i reloadData when i get back into VCAuthor. I just NSLog the authorName before and after the undo. Before undo the authorName is the changed one Big Obama, and after undo it become Obama

推荐答案

首先,在这样的情况下,我将使用单独的MOC而不是undo管理器。也就是说,我会做这样的事情(假设ARC - 你可以做映射如果必要)...

Several things to consider. First, in a scenario like this, I would use a separate MOC instead of the undo manager. Namely, I'd do something like this (assuming ARC - you can do the mapping if necessary)...

你必须有一些其他代码提供书VC通过setter,因为你在viewDidLoad中访问它。我改变viewDidLoad到这样的...

You must have some other code providing the book to the VC through a setter, since you access it in viewDidLoad. I'd change viewDidLoad to something like this...

-(void)viewDidLoad
{
    self.managedObjectContext = [[NSManagedObjectContext alloc] init];
    self.managedObjectContext.parentContext = self.book.managedObjectContext;
    // We are in the main thread, so we can safely access the main MOC
    // Basically, move our object pointer to book into our local MOC.
    NSError * error = nil;
    Book *book = [self.managedObjectContext existingObjectWithID:self.book.objectID error:&error];
    // handle nil return and/or error
    self.book = book;
    // Now, your access to book will be in the local MOC, and any changes
    // you make to the book or book.author will all be confined to the local MOC.
}

现在,您所要做的就是调用

Now, all you have to do is call

[self.managedObjectContext save:&error];

编辑

注意,上面的保存只是将对象状态移动到父上下文中。所以,主MOC现在有从孩子的更改,但没有一个更改已保存到磁盘。

Note, that the above "save" just moves the object state into the parent context. So, the "main" MOC now has the changes from the "child" but none of the changes have been saved to disk yet.

这篇关于撤消在子视图控制器中进行的所有更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆