Reputation: 820
My application includes very similar album pages that draw pictures from web server. Every page has an album title and a table to show pictures. Only difference between pages is http request parameters. I collected common behaviours (and data) into a super class "CommonViewController" and then I created child classes (FoodsViewController, BeveragesViewController) inherited from CommonViewController. I'll call child view controllers using their "story board id" from menu and then pass http request parameters to super class CommonViewController.
I placed child view controllers to story board and created outlets to labels and table views for each child view controller using mouse (cntrl+left click).
Child view controllers fill their request parameters to a struct that is defined in CommonViewController then it fetches the pictures from server.
The outlets is located in child view controllers but data is producing in super class. CommonViewController doesn't know that which outlet will be written. How could I write data to outlets which is located in child view controllers from super class?
Upvotes: 0
Views: 414
Reputation: 114875
Once you have collected your data in CommonViewController
call a method such as [self processData:receivedData];
where receivedData
is the structure you want to process in the child - I will just use an NSData
instance for this example.
You can simulate an abstract method by having the following in your commonViewController
implementation of processData
:
CommonViewController.m:
-(void) processData:(NSData *)receivedData
{
NSAssert(NO,@"Override processData in your subclass");
}
Then in each of your subclasses, override the method to perform whatever is required:
FoodsViewController.m:
-(void) processData:(NSData *)receivedData
{
//TODO put the data into the right controls
}
Update
If the mapping of data to the visual controls is the same (i.e. there is always a "title Label" and a "item picture" for example, but their placement on the screen changes, then you can define the control properties in your superclass.h -
@property (weak,nonatomic) IBOutlet UILabel *titleLabel;
@property (weak,nonatomic) IBOutlet UIImageView *itemPicture;
Then when you create use a subclass as your UIViewController in InterfaceBuilder you can just drag the connection from the control to the IBOutlet. If you are creating your controls programatically rather than in IB, then you can just assign them to the property in the subclass -
[self.view addSubView:myTitleLabel];
self.titleLabel=myTitleLabel;
In this case you can omit the IBOutlet
Upvotes: 1