简体   繁体   中英

Delete coreData object from another view

I want to delete a coreData object from another view with an if-condition.

So in viewControllerA there's the entity Buy used with the attribute cellName . And viewControllerB contains a tableView and the coreData entity 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. 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:

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

NOTE: in your dealloc method you should remove yourself from observer:

[[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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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