我正努力将变量从一个类传递到另一个类。我已经做了一些阅读,Segue方法似乎对我来说最合适,但我无法让它工作。一点帮助将不胜感激。Xcode - Segue - 将变量从Cell类传递给TableViewController类
我有一个UITableView
(TableViewController1
),其中包含一个单元格(Cell1
),其中包含一个标签(ReferenceLabel 1
)。 Table 1
从数组中填充;每个单元格包含一个参考号码(包含在ReferenceLabelLabel 1
之内)。所选单元格的参考号码需要传递到TableViewController2
。
我到目前为止有:
TableViewController1.h
#import <UIKit/UIKit.h>
#define kGETUrl @"http://localhost/RC.php"
@interface TableViewController : UITableViewController {
NSMutableArray *json;
}
@end
TableViewController1.m
#import "TableViewController1.h"
#import "TableViewController2.h"
#import "TableViewCell1.h"
@interface TableViewController1()
@end
@implementation TableViewController1
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void) getData:(NSData *) data {
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
[self.tableView reloadData];
}
- (void) start {
NSURL *url = [NSURL URLWithString:kGETUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
#pragma mark - View Lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[self start];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [json count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableViewCell1";
TableViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell1 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *info = [json objectAtIndex:indexPath.row];
cell.ReferenceLabel1.text = [info objectForKey:@"id"];
return cell;
}
//I'm stuck with the following... any help appreciate... i cant figure how to send the selected row's ReferenceLabel.text value
-(void)TableViewController1:(UITableView *)TableViewController1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"TVC1RefNumberTVC2" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"TVC1RefNumberTVC2"])
{
NSIndexPath *ip = [self.tableView indexPathForSelectedRow];
DetailObject *detail = [self detailForIndexPath:path];
[segue.destinationViewController setDetail:detail];
}
}
@end
至于TableViewController2
,我猜我只需要为变量创建我的属性m从TableViewController1
发送?