Ios 在目标c中使用全局变量

Ios 在目标c中使用全局变量,ios,objective-c,json,uitableview,global-variables,Ios,Objective C,Json,Uitableview,Global Variables,如何在objective-c中将数据从一个视图控制器传递到另一个视图控制器?我知道在swift中,我会在类声明之外创建一个全局变量,但在objective-c中,是否有任何方法将数据从所选单元格传递到用户选择该单元格时显示的新视图 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell =

如何在
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中生成字符串变量的属性

@property(nonatomic,strong) NSString *str;
按下ViewController2和ViewController3时:

ViewController2 *viewController = [ViewController2 alloc]init];
viewController2.str = @"Some text";
[self.navigationController pushViewController:viewController2 animated:YES];

您可以从ViewController2中的ViewController1发送数据。

考虑将字符串数据从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];

您可以从ViewController2中的ViewController1发送数据。

您需要更改
didSelectRowAtIndexPath
如下所示

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self performSegueWithIdentifier:@"detailSeg" sender:indexPath];
}
如果在
prepareforsgue
方法中有带
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;
  }
}

您需要像这样稍微更改
didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self performSegueWithIdentifier:@"detailSeg" sender:indexPath];
}
如果在
prepareforsgue
方法中有带
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,则可以使用类对象访问创建的属性,如下所示

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

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

以下步骤提供了将数据从一个
UIViewController
传递到另一个
UIViewController
的帮助

步骤1:在第二个
UIViewController
(.h文件)中向objective-c对象声明属性。对象可以是任何类似于
NSDictionary
NSArray
等的对象。在这里,我为
NSString
创建了属性

@property (nonatomic, retain) NSString *strTitle;
步骤2:当您从一个
UIViewController
转到另一个
UIViewController
时,您拥有另一个
UIViewController
的对象。拥有对象后,如果创建了
属性
步骤1,则可以使用类对象访问创建的属性,如下所示

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

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

为什么要在Swift中使用全局变量?语言的选择不应该改变方法。在这两种语言中,全局变量都是传递数据的错误方式。为什么要在Swift中使用全局变量?语言的选择不应该改变方法。在这两种语言中,全局变量都是传递数据的错误方式。