views:

63

answers:

2

I have an UIViewController subclass that allows the user to add a new model object, in this case a bank account. I named the view controller AddBankAccountViewController. The user can enter the account number, an account label and the bank of the account, but this is all done in separate view controllers.

I'm using delegation to let AddBankAccountViewController know that the user has changed values. The result is that AddBankAccountViewController now conforms to 3 custom made protocols (ie. EditAccountNumberViewControllerDelegate, etc.), and it may be even more in the future.

My question is: is this the correct way to do this, or is there a better way (for instance a direct link from the view controllers to the AddBankAccountViewController)?

+1  A: 

Delegation is fine. You might want to sum related methods up in a common (maybe optional) protocol. Or you could use target/action pattern, but delegation is probably what you want most of the time.

Eiko
+1  A: 

An alternative option is to have a BankAccount class and modify it directly, and have BankAccount send out notifications when it's changed (or just assume it might have changed in viewDidAppear:). Then just pass the bank account to modify to each view controller.

tc.
AKA Key-Value Observing. http://developer.apple.com/iphone/library/iPad/index.html#documentation/Cocoa/Conceptual/KeyValueObserving/Concepts/Overview.html
falconcreek
Not quite; I've *never* used KVO because it just seemed like unnecessary complexity (the documentation is a bit vague), especially when UIKit uses "normal" notifications, e.g. UIDeviceOrientationDidChangeNotification instead of KV-observing UIDevice's "orientation". I use stuff like `[[NSNotificationCenter defaultCenter] postNotificationName:MyClassFooChangedNotification object:self]`.
tc.
There is always more than one way... @synthesized properties that are KVC compliant will post notifications automatically.
falconcreek
The other problem is that KVO notifications all get delivered to the same method, so you have to figure out what's going on yourself.
tc.