从plist传递segue中的数据。表视图到详细信息视图

时间:2014-07-22 15:46:26

标签: ios objective-c tableview segue

我想将plist中包含的数据从表视图传递到详细信息视图控制器。我用硬编码的值来做,但是当我使用plist时,我无法通过segue传递它。

这是我的 Wine.h 类:

@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 文件

@property (strong, nonatomic) NSMutableArray *wineList;
@property (strong, nonatomic) NSMutableArray *thumbnails;
@property (strong, nonatomic) NSMutableArray *information;

RedWinesDetailViewController.h 文件:

@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (strong, nonatomic) IBOutlet UILabel *name;
@property (strong, nonatomic) IBOutlet UITextView *information;

@property (strong, nonatomic) Wine *wine;
位于 RedWinesViewController.m 文件

内的

prepareForSegue

- (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);
    }
}
RedWinesDetailViewController

中的

viewDidLoad

self.title = self.wine.name;
NSLog(@"Name value is %@ - %@", self.title, self.wine.name);

此时值为空。我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

简短而简单的答案是你需要构造一个Wine对象并将其传递给你的代码使用它。例如:

- (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;
    }
}

但是,在进一步检查代码时,您使用的是三个阵列!您可以将其合并为一个:winesList

viewWillAppear中你应该做的事情如下:

for (int i = 0; i < winesCount; i++)
{
  Wine *curWine = [Wine new];
  curWine.name = ;
  curWine.information = ;
  curWine.thumbnail = ;
  [self.winesList.addObject:curWine];
}

所以你自己构建了一系列葡萄酒。你为什么这样做?因为在cellForRowAtIndexPath中,您可以执行以下操作:

{
   Wine *cellWine = [self.winesList objectAtIndex:indexPath.row];
   cell.nameLabel.text = cellWine.name;
   cell.thumbnailImageView = cellWine.thumbnail;
   cell.informationLabel.text = cellWine.information;
}

最后,在准备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;
    }
}

了解您不再需要使用三个不同的阵列?您不需要确保每个具有相同的计数,排序相同,等等。创建每个wine对象而不是将所有信息存储在不同的数组中,然后只存储一个wine对象的数组。它将使您的代码更清晰,更紧凑,更少混淆,并且更不容易出错!