2012-02-08 97 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

相關問題