Passing data in segue from plist. Table view to Details View

Mariyan

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?

LyricalPanda

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing Parsed Data from the Parent view to Child view using segue

From Dev

Passing data from table view to view controller

From Dev

Passing Data with without Segue from Container View to MainVC

From Dev

Passing data between View Controllers using Segue

From Dev

Passing Data between View Controllers without segue

From Dev

Passing data on segue to view controller (Swift)

From Dev

Passing Object from Table View Cell to Another View Controller Via Segue

From Dev

Passing data from table view cell to new view controller error

From Dev

Passing Data between view Controllers Using a segue from a view embedded in a navigation controller to a tabbarcontroller

From Dev

Multiple Segue from Table View Controller

From Dev

detail view not updated when Passing data from table view to detail view

From Dev

segue table view to view controller and table view

From Dev

Passing Data To Container View From Parent View

From Dev

Passing data from View to another View

From Dev

Passing data into a table view controller from a menu selection in a storyboard

From Dev

Passing data from Table view cell using button delegate

From Dev

Passing data to Segment control from Table view Cell

From Dev

Perform segue click button from collection view inside table view

From Dev

How to segue into a new view controller from button in table view cell

From Dev

perform segue from collection view inside table view

From Dev

passing tapped cell data to another view via segue in Swift

From Dev

Passing JSON objects from one table view to another table view

From Dev

Passing and accessing object data outside table view

From Dev

laravel passing data from view from controller

From Dev

Passing Data between view Controller and a Table View Controller

From Dev

Passing data of table view (row select) to view controller

From Dev

Passing value from table view controller to view controller

From Dev

How to create a segue from the disclosure indicator on a table view to another viewController?

From Dev

Segue from Table View Cell xib with disclosure Indicator to storyboard

Related Related

  1. 1

    Passing Parsed Data from the Parent view to Child view using segue

  2. 2

    Passing data from table view to view controller

  3. 3

    Passing Data with without Segue from Container View to MainVC

  4. 4

    Passing data between View Controllers using Segue

  5. 5

    Passing Data between View Controllers without segue

  6. 6

    Passing data on segue to view controller (Swift)

  7. 7

    Passing Object from Table View Cell to Another View Controller Via Segue

  8. 8

    Passing data from table view cell to new view controller error

  9. 9

    Passing Data between view Controllers Using a segue from a view embedded in a navigation controller to a tabbarcontroller

  10. 10

    Multiple Segue from Table View Controller

  11. 11

    detail view not updated when Passing data from table view to detail view

  12. 12

    segue table view to view controller and table view

  13. 13

    Passing Data To Container View From Parent View

  14. 14

    Passing data from View to another View

  15. 15

    Passing data into a table view controller from a menu selection in a storyboard

  16. 16

    Passing data from Table view cell using button delegate

  17. 17

    Passing data to Segment control from Table view Cell

  18. 18

    Perform segue click button from collection view inside table view

  19. 19

    How to segue into a new view controller from button in table view cell

  20. 20

    perform segue from collection view inside table view

  21. 21

    passing tapped cell data to another view via segue in Swift

  22. 22

    Passing JSON objects from one table view to another table view

  23. 23

    Passing and accessing object data outside table view

  24. 24

    laravel passing data from view from controller

  25. 25

    Passing Data between view Controller and a Table View Controller

  26. 26

    Passing data of table view (row select) to view controller

  27. 27

    Passing value from table view controller to view controller

  28. 28

    How to create a segue from the disclosure indicator on a table view to another viewController?

  29. 29

    Segue from Table View Cell xib with disclosure Indicator to storyboard

HotTag

Archive