从不同的View Controller访问变量

时间:2012-07-20 08:36:06

标签: ios xcode uitableview variables storyboard

我开发了一个应用程序,使用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];     
    }  
}

3 个答案:

答案 0 :(得分: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和细节控制器之间创建一个segue并使用那个segue

- (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];
  }   
 }

答案 1 :(得分:0)

有故事板

How pass data in seque ios5 storyboard uitableview to detail view

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


没有故事板 为此,您必须在第一类中访问varible,请参阅下面的示例,例如,在单击表行时,您必须在另一个视图中导航,例如根视图。因此,为此,在根视图中定义变量,并在表的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]
}

答案 2 :(得分:0)

假设您在detailedVC strNamestrDescription中有2个属性...

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

detailedVCObject.strName = @"whatever";
detailedVCObject.strDescription = @"whatever";
希望它有所帮助...