2012-02-08 150 views
1

我有两个UITableViewController类,MainTableController和SubTableController。从一个表插入数据到另一个表

从AppDelegate类我打电话给MainTableController类。

起初这个类是空的,并且在这个类中有一个名为“show list”的按钮。 当我点击这个按钮时,我将进入SubTableController,在那里我有一个表格形式的动作列表。
现在,如果我选择第一个单元格操作,那么该操作名称必须位于MainTableController中的第一个表格单元格中。但是我无法在MainTableController类的表格中打印该名称。

在SubTableController:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ActionList * actionListObj = [appDelegate.actionArray objectAtIndex:indexPath.row]; 
    self.chooseActions = actionListObj.actionName; 
    MainTableController * mainViewController = [[MainTableController alloc] init];  
    [mainViewController getAction:self.chooseActions]; 
    [self.navigationController dismissModalViewControllerAnimated:YES]; 
} 

在MainTableController:

-(void) viewWillAppear:(BOOL)animated{ 
    [self reloadData]; 
} 

-(void) reloadData{ 
    [self.myTableView reloadData]; 
} 

-(void) getAction: (NSString *) actionChoose{   
    self.action = actionChoose; 
    [self reloadData]; 
}  

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell";  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    // Configure the cell... 
    cell.textLabel.text = self.action; 
    return cell; 
} 

当我调试,在MainTableController我得到的的getAction方法,但在表格单元格中的文本串的动作为空。

任何人都可以请帮我关于这个吗?我哪里错了?

回答

1

您正在分配并初始化新视图控制器每次您在SubTableController中选择一个单元时。

MainTableController * mainViewController = [[MainTableController alloc] init]; 

当然,它并不是导航堆栈中的一个。

您需要使这两个控制器进行通信。
我建议子视图控制器在主要视图控制器上定义一个属性,以便在需要时发送消息。

SubTableController,添加属性和合成它:

@property(readwrite, assign) MainViewController *mainViewController; 
// and ... 
@synthesize mainViewController; 

当然,当你推子视图控制器,不要忘记设置该属性。

// in main view controller 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // alloc/init the sub VC 
    subVCInstance.mainViewController = self; 
    [self pushViewController:subVCInstance ...... 

现在,当一列中的子一个被选中,消息主要的一个,不分配/初始化一个新的MainViewController对象:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ActionList * actionListObj = [appDelegate.actionArray objectAtIndex:indexPath.row]; 
    self.chooseActions = actionListObj.actionName; 
    //MainTableController * mainViewController = [[MainTableController alloc] init];  
    [self.mainViewController getAction:self.chooseActions]; 
    [self.navigationController dismissModalViewControllerAnimated:YES]; 
} 

这应该只是罚款。

+0

文斯 - 它没有工作:(我想你说。但是我面对的这个问题是MainTableController不是委托类,它是一个tablViewController类。所以请给我一些更多的提示,请.. – hgpl 2012-02-08 10:28:46

+0

我看不出任何理由为什么这是行不通的。不管什么类型的'mainViewController',你是否在推送时设置了子VC的属性? – 2012-02-08 10:32:27

+0

是的,我做到了。我喜欢这个。在SubTableController.h文件中,MainActionViewController * mainViewController; @property(读写,分配)MainActionViewController * mainViewController;并在SubViewController.m中,[self.mainViewController getAction:self.chooseActions];在didSelectRowAtIndexPath:方法 – hgpl 2012-02-08 10:34:03

1

SubTableController类的didSelectRowAtIndexPath中,您正在分配新的MainTableController来发送数据。

MainTableController * mainViewController = [[MainTableController alloc] init];  
[mainViewController getAction:self.chooseActions]; 

你不应该那样做。因为现有的主视图将与新分配的主视图不同。您应该使用delegate来回馈数据。有关协议和委托的详细信息,see here

More example here

相关问题