I want to pass data contained in plist from a table view to details view controller. I did it with hardcoded values, but when I'm using plist I have trouble passing it through the segue.
Here is my Wine.h class:
@property (strong, nonatomic) NSString *name; //name of the wine
@property (strong, nonatomic) NSString *image; //image of the wine
@property (strong, nonatomic) NSString *information; // details for the wine
RedWinesViewController.h file
@property (strong, nonatomic) NSMutableArray *wineList;
@property (strong, nonatomic) NSMutableArray *thumbnails;
@property (strong, nonatomic) NSMutableArray *information;
RedWinesDetailViewController.h file:
@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (strong, nonatomic) IBOutlet UILabel *name;
@property (strong, nonatomic) IBOutlet UITextView *information;
@property (strong, nonatomic) Wine *wine;
prepareForSegue located inside RedWinesViewController.m file
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showRedWineDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RedWinesDetailViewController *destViewController = segue.destinationViewController;
destViewController.wine.name = [self.wineList objectAtIndex:indexPath.row];
destViewController.wine.information = [self.information objectAtIndex:indexPath.row];
//destViewController.wine.image = [self.thumbnails objectAtIndex:indexPath.row];
NSLog(@"Value is %@", destViewController.wine.name);
}
}
viewDidLoad in RedWinesDetailViewController
self.title = self.wine.name;
NSLog(@"Name value is %@ - %@", self.title, self.wine.name);
At this point the values are null. What am I doing wrong in here?
The short and simple answer is you need to construct a Wine object and pass it through to use it with your code. For example:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showRedWineDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Wine *selectedWine = [Wine new];
selectedWine.name = [self.wineList objectAtIndex:indexPath.row];
selectedWine.information = [self.information objectAtIndex:indexPath.row];
RedWinesDetailViewController *destViewController = segue.destinationViewController;
destViewController.wine = selectedWine;
}
}
However upon further review of your code, you are using three arrays! You can combine this down to one: winesList
.
In viewWillAppear
you should be doing something like:
for (int i = 0; i < winesCount; i++)
{
Wine *curWine = [Wine new];
curWine.name = ;
curWine.information = ;
curWine.thumbnail = ;
[self.winesList.addObject:curWine];
}
So you construct yourself an array of wines. Why do you do this though? Because in your cellForRowAtIndexPath
, you can simply do:
{
Wine *cellWine = [self.winesList objectAtIndex:indexPath.row];
cell.nameLabel.text = cellWine.name;
cell.thumbnailImageView = cellWine.thumbnail;
cell.informationLabel.text = cellWine.information;
}
And finally, in your prepare for segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showRedWineDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Wine *selectedWine = [self.winesList objectAtIndex:indexPath.row];
RedWinesDetailViewController *destViewController = segue.destinationViewController;
destViewController.wine = selectedWine;
}
}
See how you no longer need to use three different arrays? You don't need to make sure each have the same count, are ordered the same, etc. Create each wine
object instead of storing all it's information in different arrays, then just store an array of the wine objects. It'll make your code a lot cleaner, more condense, less confusing, and less prone to bugs!
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments