【问题标题】:Using global variables in objective c在目标 c 中使用全局变量
【发布时间】:2015-04-22 04:11:43
【问题描述】:

如何在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];
}

【问题讨论】:

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

标签: ios objective-c json uitableview global-variables


【解决方案1】:

考虑你想从 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 发送的数据。

【讨论】:

    【解决方案2】:

    你需要像这样改变你的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;
      }
    }
    

    【讨论】:

      【解决方案3】:

      以下步骤有助于将数据从一个 UIViewController 传递到另一个 UIViewController

      第 1 步: 在第二个 UIViewController(.h 文件)中向 Objective-c 对象声明属性。该对象可能是NSDictionaryNSArray 等。这里我为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";
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-31
        相关资源
        最近更新 更多