在 objective c 中使用全局变量

Using global variables in objective c

如何在 objective-c 中将数据从一个视图控制器传递到另一个视图控制器?我知道在 swift 中我会在 class 声明之外创建一个全局变量,但是在 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中对字符串变量进行属性

@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 对象。该对象可以是 NSDictionaryNSArray 等任何东西。我在这里为 NSString.

创建了 属性
@property (nonatomic, retain) NSString *strTitle;

第 2 步: 当您从一个 UIViewController 转到另一个 UIViewController 时,您拥有另一个 UIViewController 的对象。一旦你有了对象,并且如果你像 step 1 那样创建了 property,那么你可以使用 class 对象访问创建的 属性,如下所示。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"detailSeg"]) {

        DetailViewController * objDetails = (DetailViewController * )segue.destinationViewController;
        objDetails.strTitle = "Hello";
    }
}