如何在objective-c
中将数据从一个视图控制器传递到另一个视图控制器?我知道在swift
中我只会在类声明之外创建一个全局变量,但是objective-c
有没有办法将数据从所选单元格传递到用户选择该单元格时显示的新视图?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BarInfoCell"];
[cell textLabel].text = [[bars objectAtIndex:indexPath.row] objectForKey:@"name"];
NSString *barAddress = [NSString stringWithFormat: @"%@ %@ %@", [[bars objectAtIndex:indexPath.row] objectForKey:@"address"],
[[bars objectAtIndex:indexPath.row] objectForKey:@"state"],
[[bars objectAtIndex:indexPath.row] objectForKey:@"zip"]];
[cell detailTextLabel].text = barAddress;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performSegueWithIdentifier:@"detailSeg" sender:self];
}
考虑您要从 ViewController1 将字符串数据发送到 ViewController2,ViewController3。
在 ViewController2 和 ViewController3 中生成字符串变量的 make 属性。
@property(nonatomic,strong) NSString *str;
在推动 ViewController2 和 ViewController3 时:
ViewController2 *viewController = [ViewController2 alloc]init];
viewController2.str = @"Some text";
[self.navigationController pushViewController:viewController2 animated:YES];
并且您有从ViewController1发送的数据在ViewController2中。
你需要像这样稍微改变你的didSelectRowAtIndexPath
,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performSegueWithIdentifier:@"detailSeg" sender:indexPath];
}
您可以将数据从一个视图发送到另一个视图,如果prepareForSegue
方法中存在与UINavigationController
的 segue,例如,
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"detailSeg"]) {
NextViewContorller nVC = (NextViewContorller)segue.destinationViewController;
NSIndexPath *ip = (NSIndexPath *)sender;
//You need to define variable in which you want to pass data, in this case value1,value2
//WAY 1
NSString *barAddress = [NSString stringWithFormat: @"%@ %@ %@", [[bars objectAtIndex:ip.row] objectForKey:@"address"],
[[bars objectAtIndex:ip.row] objectForKey:@"state"],
[[bars objectAtIndex:ip.row] objectForKey:@"zip"]];
nVC.value1 = [[bars objectAtIndex:ip.row] objectForKey:@"name"]
nVC.value2 = barAddress;
//OR
//WAY 2
UITableViewCell *cell = [self.tableview cellForRowAtIndexPath:ip];
nVC.value1 = cell.textLabel.text;
nVC.value2 = cell.detailTextLabel.text;
}
}
将数据从一个UIViewController
传递到另一个UIViewController
的步骤。
第 1 步:在第二个UIViewController
(.h 文件)中将属性声明为 objective-c 对象。对象可以是NSDictionary
、NSArray
等,在这里我为NSString
创建了属性。
@property (nonatomic, retain) NSString *strTitle;
步骤2:当您从一个UIViewController
转到另一个UIViewController
时,您有另一个UIViewController
的对象。 一旦你有对象,如果你创建了像步骤1这样的property
,那么你可以使用类对象访问创建的属性,如下所示。
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"detailSeg"]) {
DetailViewController * objDetails = (DetailViewController * )segue.destinationViewController;
objDetails.strTitle = "Hello";
}
}