简体   繁体   English

从另一个视图删除coreData对象

[英]Delete coreData object from another view

I want to delete a coreData object from another view with an if-condition. 我想从另一个带有if条件的视图中删除coreData对象。

So in viewControllerA there's the entity Buy used with the attribute cellName . 因此,在viewControllerA中,实体Buy与属性cellName一起使用 And viewControllerB contains a tableView and the coreData entity List . 而且viewControllerB包含一个tableView和coreData实体List When the user deletes a cell in viewControllerB the object of viewControllerA which has cellName (viewControllerA) = name of the deleted cell (viewControllerB) should also be deleted. 当用户删除viewControllerB中的单元格时,具有cellName(viewControllerA)=删除的单元格名称(viewControllerB)的viewControllerA对象也应删除。 Maybe someone could help me... 也许有人可以帮我...

There are probably a couple of options including a custom delegate but a possibility to start would be through notifications 可能有几个选项,包括自定义委托,但是可以通过通知来启动

In your viewControllerA you would register for a notification in viewWillAppear or viewDidLoad: 在您的viewControllerA中,您可以在viewWillAppear或viewDidLoad中注册一个通知:

    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(shouldUpdateDisplay:) 
    name:@"SHOULD_UPDATE_DISPLAY"
    object:nil];

NOTE: in your dealloc method you should remove yourself from observer: 注意:在您的dealloc方法中,您应该将自己从观察者中移除:

[[NSNotificationCenter defaultCenter] removeObserver:self];

Then implement the method: 然后实现该方法:

- (void) shouldUpdateDisplay:(NSNotification *) notification
{
     [_table reloadData]; // do your updates 
}

In VCB you would send that notification when an element was deleted and the other view controller should know about it: 在VCB中,当元素被删除时,您将发送该通知,而另一个视图控制器应该知道该通知:

[[NSNotificationCenter defaultCenter] 
    postNotificationName:@"SHOULD_UPDATE_DISPLAY" 
    object:self];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM