passing data/objects/moc between viewcontrollers - best practice

后端 未结 1 1602
醉酒成梦
醉酒成梦 2021-01-07 08:37

I have a scenario that i suspect is very common, i\'ve found various ideas in responses to other similar questions including setting up IBOutlets, passing NSmanagedobjects a

相关标签:
1条回答
  • 2021-01-07 09:16

    I would use notifications for this. Notifications are a simple way to decouple parts of the application, but still have them work together.

    In your case, the moment the user selects the tag object in the 3rd view, I would send a notification as such:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"tagSelected" object:myTag];
    

    Now, in the controller that has the "add" button, make it subscribe to that event:

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

    Make sure you implement the handleTagSelected: method, and in that method you can get the tag object and close the drilldown view that you have open:

    - (void)handleTagSelected:(NSNotification *)notification {
        Tag *mytag = (Tag *)notification.object;
        [self dismissModalViewControllerAnimated:YES];
    }
    

    Then you can do whatever you want with the tag.

    0 讨论(0)
提交回复
热议问题