简体   繁体   中英

Can two objects be delegates of each other…?

Is it ok to have two objects as delgates of each other..? My scenario is that I have a view which is shown in two modes.. first: Create mode and second: Edit mode

In Create Mode all the fields are empty and I take data from the view which is filled in by the user and I update my data model.

And In View Mode I fill up the view from my data model.

This is being done using a spilt view controller(because of this I am forced to use delegation). I wish I could explain this better but this is the best I can do. As of now I am using delegation to communicate from A to B and Notification from B to A.

Would this work fine if I use delegation in both ways... or are there any complexities involved which I can't foresee?

There are a few problems that could occur, but if you take the necessary precautions, it will be fine:

  1. Ensure both delegates are weak referenced. This means using @property (weak) on ARC or @property (assign) . This will prevent retain cycles from occurring.

  2. Ensure you don't get into a situation where a delegate method calls the delegate method of the other controller, which calls the same delegate method in the first controller and so on. You could easily get an infinite loop if you are not careful.

A discussion or debate on whether or not this is the best design pattern in this situation is not really something that belongs on SO. But doing it this way is possible if you are careful, which is the answer to your question.

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