objective c - How to pass parameters to controller in object oriented style -
when photo clicked check it's category , phone call http request function , configure parameters according photos category. here simplified code:
- (void) tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { item *item = (item *) [self.recentitems objectatindex:indexpath.row]; if ( [item.type isequal: @"typea"] ) { connectionproperties.p1 = "a1" connectionproperties.p2 = "a2" } else if ( [item.type isequal: @"typeb"] ){ connectionproperties.p1 = "b1" connectionproperties.p3 = "b2" connectionproperties.p4 = "b3" } } albumdatacontroller = [[albumdatacontroller alloc] initwithconnectionproperty:connectionproperties andcommunicator:self.comm]; [albumdatacontroller fetchitemsforcategory:category itemssuccess:^(album *album) { photos = [[nsarray alloc] initwitharray:album.photos]; photoviewcontroller *photoviewcontroller = [[photoviewcontroller alloc] initwithphotos:photos]; [self presentviewcontroller: photoviewcontroller animated:yes completion:nil]; } }
i know not object oriented way. how should realize in object oriented way.
the object-oriented approach have subclasses of item
create own connection properties (or improve yet have item
protocol since objective-c doesn't have abstract methods).
example:
@interface item : nsobject - (connectionproperties *)connectionproperties; @end @implementation item - (connectionproperties *)connectionproperties { [self doesnotrecognizeselector:_cmd]; homecoming nil; } @end @interface itema : item @end @implementation itema - (connectionproperties *)connectionproperties { connectionproperties *connectionproperties = [[connectionproperties alloc] init]; connectionproperties.p1 = "a1"; connectionproperties.p2 = "a2"; homecoming connectionproperties; } @end @interface itemb : item @end @implementation itemb - (connectionproperties *)connectionproperties { connectionproperties *connectionproperties = [[connectionproperties alloc] init]; connectionproperties.p1 = "b1" connectionproperties.p3 = "b2" connectionproperties.p4 = "b3" homecoming connectionproperties; } @end
this way code doesn't need know internals of items:
- (void) tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { item *item = (item *) [self.recentitems objectatindex:indexpath.row]; albumdatacontroller = [[albumdatacontroller alloc] initwithconnectionproperty:item.connectionproperties andcommunicator:self.comm];
using protocol similar:
@protocol item <nsobject> - (connectionproperties *)connectionproperties; ... @end
objective-c oop object-oriented-analysis
No comments:
Post a Comment