I'm trying to get my head around accessing the model data that is to be displayed in a tableview and how, exactly, I can provide that data to the view controller from another part of my application. I think I have a pretty good understanding of how table views work when the MSMutableArray that holds the data is part of the Table View Controller interface. It's when the data I'd like to display is initialized somewhere else that I begin to become somewhat confused.
In my application the data that I eventually want to display in a table view is read in and processed from an XML file. For the purposes of this question, it looks something like this:
//
// bunchOfData.h
//
@interface bunchOfData : NSObject
@property (strong) Profile *overview;
@property (strong) NSMutableArray *info;
@end
The data hangs out in 'bunchOfData' and is used throughout the application for a variety of tasks.
I've provided a menu that will allow the user to display the data from the 'info' MSMutableArray in a new window that contains a table view. The menu is defined in a storyboard, as is the window containing the NSTableView, and I've hooked up a segue from the menu item to the window as you would expect.
Here's the header for the table view controller:
//
// TableViewController.h
//
@interface TableViewController : NSViewController <NSTableViewDataSource> {
@private
IBOutlet NSTableView *tableView;
bunchOfData *data;
}
@end
And the source:
@implementation TableViewController
- (id)init
{
self = [super init];
if (self)
data = // Hmmm.... I'd like to assign 'info' to this
return (self);
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [data.info count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
:
:
return [value for the object in the table];
}
@end
Now, were I'm a little lost is how to get a reference to the 'info' array in bunchOfData.h to my table view controller (as noted by the 'data =' placeholder in the source file.
How can I initialize the table view controller with a reference to the data inside bunchOfData, when the window containing the table view is being opened by a menu hooked up to the window through a storyboard? It seems as though the storyboard to hiding a lot of necessary details, and I'm unsure how to get to those details.
Thanks!
I believe the right way to get the data to your view controller is to use the prepareForSegue:sender:
function from the originating view controller. If you implement this function in the view controller where you have your menu, it will be called right before control is transferred to the new view controller.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.destinationViewController isKindOfClass:[TableViewController class]]) {
TableViewController *vc = (TableViewController *)segue.destinationViewController;
vc.data = info;
}
}
The if
statement is to make sure we are operating on the right segue. If you have several segues that you want to pass data to, you can add several if
-else if
-else if
to process each one of them in the same function. Instead of checking the view controller class you can check the segue's identifier. This is useful if you have several segues that connects to view controllers with the same class. The identifier is set in your storyboard from interface builder.
Another handy function worth mentioning is shouldPerformSegueWithIdentifier:sender:
which you can use to cancel a segue with.
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([identifier isEqualToString:@"SegueA"] && !self.allValuesGood)
return NO;
return YES;
}