2013-02-16 59 views
1

我對此很陌生。我有一個Tableview應用程序,當我按下一個單元格時,它將導航到一個DetailView。現在的問題是,我在Detailview中有一個textField,我想在用戶按下保存按鈕後加載用戶已經回到原始單元格的detailTextLabel段的數據。如何將數據從DetailView傳遞給TableView?

我該如何去做?下面是我有:

DetailViewController.m

//save button 
-(IBAction) SendTextFieldtoViewController:(id)sender { 

    NSString *grabKeyedData = self.inputField.text; 
    ViewController *mainController =[self.storyboard instantiateViewControllerWithIndentfier:@"mainController"]; 

    [mainController GrabbedKeyData: grabKeyedData]; 
} 

ViewController.m

-(void) GrabbedKeyData:(NSString *)text{ 

    grabData = text; 

    NSLog(@" data: %@",text); 
    [tableView reloadData]; 
} 


-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath { 

..... 

    if(grabData == nil){ 
     [[cell detailTextLabel] setText:@"no data"]; 
    }else{ 
     [[cell detailTextLabel] setText:grabData]; 
    } 

我已成功地將數據傳遞到 - (無效)GrabbedKeyData,我可以從NSLog讀取傳遞的數據。但是當我返回到ViewController時,表似乎並沒有從它更新。

+0

你在使用故事板嗎?你是如何從ViewController導航到詳細視圖控制器的? – rdelmar 2013-02-16 16:58:10

回答

1

我認爲這個問題可能是兩件事之一。在你的問題中,你說「返回到ViewController」,但你不會回到你開始的那個,你正在創建一個新的。如果你通過在故事板中從ViewController推送到細節控制器,那麼這不是實現它的方式。您需要獲取對ViewController原始實例的引用,或者使用協議(這是Apple建議將數據發送回控制器的標準方式)。有幾種方法可以獲得該引用,您可以在詳細控制器中擁有一個名爲mainController的屬性,並且ViewController可以在推送詳細控制器時將其自身設置爲該屬性的值。

另一個可能的問題是一個簡單的問題 - 該表使用的是普通單元而不是具有detailTextLabel的表格。確保你在IB中設置了正確的單元格。

+0

嗨,我認爲這可能是你提到的第一個問題。但是在實現協議並使用[[self delegate] GrabbedKeyData:grabKeyeddata]之後;我的 - (void)GrabbedKeyData不再讀取文本值。 – Cheng 2013-02-17 05:29:28

+0

您應該更新您的問題以顯示您現在使用的代碼。 – rdelmar 2013-02-17 06:11:36

+0

非常感謝你,我成功地將數據傳回第一個控制器。我已經按照這個鏈接http://www.youtube.com/watch?v=pp0nF2Ti7r0的教程 – Cheng 2013-02-17 07:06:12

0
-(IBAction) SendTextFieldtoViewController:(id)sender 
{ 
    NSString *grabKeyedData = self.inputField.text; 
    [mainController GrabbedKeyData: grabKeyedData]; 
    ViewController *mainController =[self.storyboard instantiateViewControllerWithIndentfier:@"mainController"]; 
} 


-(void) GrabbedKeyData:(NSString *)text 
{ 
    self.keyDataArray = [[NSMutableArray alloc] initWithObjects:text, nil]; 
    NSLog(@" data: %@",text); 
    [tableView reloadData]; 
} 

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath { 

..... 

    NSString *grabbedName = [keyDataArray objectAtIndex:0]; 
    NSLog(@" cell text is :%@",grabbedName); 

    [[cell detailTextLabel] setText:grabbedName]; 
} 

您只將NSString對象存儲到array.so中,它存儲在第0個索引處的數組中。

+0

嗨帕夫恩。感謝您的幫助。即使我直接將文本指向表格,我的問題仍然存在。在重新加載表格後,我的表格仍然看起來像默認格式(沒有小標題)。 – Cheng 2013-02-16 14:08:19

相關問題