cocoa touch - Using Protocols in Objective C to Transfer Data Between Different Objects? -
hey guys, have root table view has toolbar @ bottom , has labels , refresh button within it, mail app's toolbar. root table view controller obtains data server allocating , initializing dataupdater class. within class nsurlconnection delegate methods called while communicating server.
as can guess, need know when (delegate) functions called within dataupdater class , values of parameters passed these delegate functions can update labels on toolbar accordingly (i.e. connecting..., updated, etc).
the problem having determining how notify root table view controller of going on in these delegate methods. use protocols, if how? have been skimming documentation , don't quite see how effect. or suggest implement program way?
thanks in advance!
a protocol kind of contract says: i promise provide non-optional methods defined in protocol, , maybe optional ones. it's purpose java interfaces: work around missing multiple-inheritence.
the delegate pattern in objective-c works this: define protocol, , in class, define variable id<myprotocol> mydelegate;
, define setter , maybe getter (either via normal methods, e.g. - (void)setdelegate:(id<myprotocol>)adelegate;
or via properties.
note delegate not retained ! if work property, need assign
option, not retain
.
now in class, check whether mydelegate
nil , if not, can directly call non-optional methods. if want call optional method, first need verify presence via respondstoselector:
.
so if decide use delegate pattern, need define protocol, add protocol root table view controller, implement necessary methods there, , make sure call [foo setdelegate:self];
or similar inform other class root table view controller delegate. , of course implement delegate calls in class.
edit:
an alternative might use nsnotification
s, btw. advantage of notifications can have multiple objects listen , react them. disadvantage cannot (directly) pass values back. example, can define delegate method asks delegate whether or not. that's not possible notifications, it's more shouting room instead of having one-to-one conversation.
Comments
Post a Comment