【问题标题】:Accessing variable from different View Controllers从不同的视图控制器访问变量
【发布时间】:2012-07-20 08:36:06
【问题描述】:

我使用TableView 开发了一个应用程序,该应用程序连接到带有搜索栏的 SQLite 数据库,并且一切正常。
我有一个详细视图控制器,它向我显示有关单击行的信息。如何将变量从AuthorVC.m 传递到Details.m

部分代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{   
    NSLog(@"prepareForSegue: %@", segue.identifier);  
    if ([segue.identifier isEqualToString:@"Details"]) 
        // segue.identifier value here   
    {      
        Details *dv = segue.destinationViewController;
        dv.labelText.text = author.title;
        //  author.title = dv.labelText.text ;
        // your value to pass     

        [segue.destinationViewController setLabelText:author.title];     
    }  
}

【问题讨论】:

    标签: ios xcode uitableview variables storyboard


    【解决方案1】:

    这样做:

    Create property of variable in myDetails to pass data
    Suppose i have NSString *strAuthoTitle in myDetails
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
     //Depending upon cell index pass your data differently
     myDetails *objmyDetails = [myDetails alloc] initWithNib:@"myDetails" bundle:nil] //alloc your controller for navigation 
    
     objmyDetails.strAuthoTitle = [yourArrayofAuthorTile objectAtIndex:indexPath.row];
    
     //Simarly other data can be passed
    }
    

    由于您没有提到您正在使用情节提要,因此您需要在 AuthorVC 和 Details 控制器之间创建一个转场并使用该转场

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
     {   
      //NSLog(@"prepareForSegue: %@", segue.identifier);      
      if ([segue.identifier isEqualToString:@"DetailsPass"]) // segue.identifier value here
      {
       objmyDetails = segue.destinationViewController;
       objmyDetails.strText =lblText.text; // your value to pass
       //[segue.destinationViewController setStrText:lblText.text];
      }   
     }
    

    【讨论】:

    • 我正在使用 Storyboard,所以我没有 xib 文件.. 有没有等效的方法
    • 我尝试将其放入 AuthorVC.m 中,但我确信调试器不会读取它。我从 slog 标记中删除了“//”,并且显示了 nthg....有什么想法吗?
    • 请检查已编辑的问题以查看我的实现是否在正确的轨道上
    【解决方案2】:

    假设您的详细 VC strNamestrDescription 中有 2 个属性...

    与在堆栈中推送详细的VC 之前相比,您可以简单地执行以下操作:

    detailedVCObject.strName = @"whatever";
    detailedVCObject.strDescription = @"whatever";
    

    希望对您有所帮助...

    【讨论】:

    • 能否请您多解释一下我是故事板的新手:s
    【解决方案3】:

    有故事板

    How pass data in seque ios5 storyboard uitableview to detail view

    http://kurrytran.blogspot.in/2011/10/ios-5-storyboard-and.html


    没有故事板 为此,您必须访问第一类中的变量,让我们看下面的示例,例如单击表格行时,您必须在另一个视图上导航,比如根视图。因此,为此在根视图中定义一个变量,并在表的 didselect 方法中创建对象时访问该视图。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RootViewController *rvController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];
    
    //Increment the Current View
    rvController.CurrentLevel += 1;
    
    //Set the title;
    rvController.CurrentTitle = [dictionary objectForKey:@"Title"];
    
    //Push the new table view on the stack
    [self.navigationController pushViewController:rvController animated:YES];
    
    [rv release]
    }
    

    【讨论】:

    • 问题是我正在使用带有情节提要的 IOS 5...我无法使用 initWithNibName...我没有 xib 文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多