Class T
In may main application class, Class A
, I have methods to populate other areas of the screen as, for instance, a map.
While I'm populating my table within Class T
, I would like to call the Class A
method that plots the x,y
points on the map.
Is this possible? What should be the correct way to do this?
When I though about this开发者_运维知识库 approach, I was expecting that invoking [super ...]
inside Class T
would call the Class A
methods, as this is the owner class of the instance, but ofcourse it call the parent class, in my case the UITableViewController
.
Thank you,
PedroIf A
is your main application class, you should be able to access the application instance with [UIApplication sharedApplication]
, cast it to the class A
type, and call the APIs you need to call.
Why not define a ClassAProtocol and then add a property "classADelegate" in Class T? ClassAProtocol will define a method like:
-(void)plotXYOnMapFromData:(id)someObjectContainingDataToPlot;
So in the Class T interface you will add:
@property (assign) id classADelegate;
and then when you instantiate, let's say from instanceA (instance of Class A), instanceT (instance of Class T) you will do:
instanceT.classADelegate = instanceA;
Finally inside Class T you can call the plotting method in this way:
[classADelegate plotXYOnMapFromData:myDataToPlot];
The advantage of the delegate pattern in this case is that Class T just need to know only one small piece of ClassA, which is the protocol, and ClassA is able to communicate with T thanks to its implementation of the protocol.
精彩评论