0

Can someone please advise on how to pass an array of NSStrings from a ViewController into a TableViewController. I have tried using prepareForSegue in my rootViewController

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
TableViewController *transferViewController = segue.destinationViewController;

NSLog(@"Segue %@", segue.identifier);

if ([segue.identifier isEqualToString:@"segueAffiliation"])
{
    // segue Affiliation happened
    transferViewController.titleText = @"Friendly";

}
else if ([segue.identifier isEqualToString:@"segueCategory"])
{
    // segue Category happened
}
else if ([segue.identifier isEqualToString:@"segueFunction1"])
{
    // segue Function1 happened
}
else if ([segue.identifier isEqualToString:@"segueFunction2"])
{
    // segue Function2 happened
}
else if ([segue.identifier isEqualToString:@"segueFunction3"])
{
    // segue Function3 happened
}

and I am not sure on how to pass the data, so far I have this in the TableViewController

@interface TableViewController ()

@end

@implementation TableViewController
{
NSArray *_tableData;
}

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

// Initialize table data

self.cellTitle.text = self.titleText;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...

return cell;
}
Larme
  • 24,190
  • 6
  • 51
  • 81
teejay
  • 53
  • 1
  • 4

2 Answers2

0

You just have to declare your array _tableData in your header file :

@property (strong, nonatomic) NSArray *_tableData;

And in your .m file :

@synthesize _tableData;

Currently _tableData is private.

R.Lambert
  • 1,160
  • 1
  • 11
  • 25
  • Hi, @Shawn i tried doing that and the tableview is still not showing my string I'm trying to pass from the view controller – teejay Mar 10 '14 at 10:07
  • have you set your array in your viewcontroller ? transferViewController._tableData = yourArrayOfString; – R.Lambert Mar 10 '14 at 10:09
  • Yep this is what I have done now if ([segue.identifier isEqualToString:@"segueAffiliation"]) { // segue Affiliation happened transferViewController._tableData = @[@"Joe Blogs"]; } @Shawn – teejay Mar 10 '14 at 10:13
  • Ok, I guess you have a UILabel or something similar in your cell, do you set it correctly at this point ? // Configure the cell... You must have something like cell.textLabel.text = [_tableData objectAtIndex:indexPath.row] – R.Lambert Mar 10 '14 at 10:17
  • Figured it out, wasn't configuring the cell correctly. Thank you though @Shawn – teejay Mar 10 '14 at 10:22
0

Create the property on next view controller .h and define getter and setter.

Add this property in NextVC.h on nextVC

@property (strong, nonatomic) NSArray * _tableData;

Add

@synthesize _tableData; in NextVC.m

And last

TableViewController *transferViewController = segue.destinationViewController;

    transferViewController._tableData=// your array.
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121