将NSString从View Controller传递到TableViewController

时间:2014-03-10 09:58:58

标签: ios objective-c uiviewcontroller uitableview

有人可以建议如何将一个NSStrings数组从ViewController传递到TableViewController。我尝试在rootViewController中使用prepareForSegue

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

我不知道如何传递数据,到目前为止我在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;
}

2 个答案:

答案 0 :(得分:0)

您只需在头文件中声明数组_tableData:

@property (strong, nonatomic) NSArray *_tableData;

在你的.m文件中:

@synthesize _tableData;

目前_tableData是私有的。

答案 1 :(得分:0)

在下一个view controller .h创建属性并定义getter和setter。

在NextVC上的NextVC.h中添加此property

@property (strong, nonatomic) NSArray * _tableData;

添加

NextVC.m中的

@synthesize _tableData;

最后

TableViewController * transferViewController = segue.destinationViewController;

    transferViewController._tableData=// your array.