2012-08-16 63 views
2

嘿,那裏我一直在研究一個交通應用一段時間,並且一直在這個問題上停留了一段時間。UITableView只向第二次嘗試的詳細視圖控制器發送數據

我正在使用iOS 5和故事板。基本上我有一個顯示喜愛公共汽車站位置一個UITableView,當用戶選擇一個行使用:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Favorite *favorite = [self.favoriteItems objectAtIndex:indexPath.row]; 

    stopString = favorite.title; 
    routeString = favorite.subtitle; 
} 

隨着用戶選擇然後我爲對應於一個賽格瑞製備所述細胞的停止和路線信息在我的故事板上繼續播放,推送一個使用停止名稱和路由名稱的詳細視圖控制器來顯示來自plist的時間。

我對Objective C和iOS相當陌生,所以我使用了一個我的朋友告訴我可以工作的segue,但是,這可能是問題所在。該SEGUE看起來是這樣的:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    UIViewController *destination = segue.destinationViewController; 
    if ([destination respondsToSelector:@selector(setDelegate:)]) 
    { 
     [destination setValue:self forKey:@"delegate"]; 
    } 
    if ([destination respondsToSelector:@selector(setSelection:)]) 
    { 
     NSString *route = routeString; 
     NSDictionary *selection1 = [NSDictionary dictionaryWithObjectsAndKeys:route, @"route", stopString, @"stop", nil]; 
     [destination setValue:selection1 forKey:@"selection"]; 
    } 
} 

後我DetailViewController的SEGUE我搶在視圖DidLoad停止和路線信息:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    route = [selection objectForKey:@"route"]; 
    stopName = [selection objectForKey:@"stop"]; 

    NSLog(@"stopName: %@", stopName); 
    NSLog(@"routeName: %@", route); 
} 

這裏是我的問題出現在哪裏。當我運行模擬器並在我的表格視圖中單擊一個單元格時,我被推送到DVC,但是,stopName和routeName都是空的,因此沒有信息被髮送或接收。但是,如果我回到表格並單擊另一個單元格,routeName和stopName將填充第一次單擊單元格時應發送的信息。如果我繼續這個過程,它會繼續發送先前未點擊的單元格的信息。

所以基本上信息是發送,但只有我經歷了兩次segue。很顯然,我希望它能夠發送信息並在第一時間收到信息,但是它被推遲了,並且讓我發瘋。我很感激任何人可以給我的幫助,因爲我一直在搜索互聯網幾天,試圖解決這個問題,非常感謝您的幫助!

回答

6

prepareForSegue:didSelectRowAtIndexPath:之前被調用。這就是爲什麼你看到的價值總是滯後。

更好的解決方案是獲取prepareForSegue:方法中的stopString和routeString值(並且完全不使用didSelectRowForIndexPath:)。這樣做的關鍵是要意識到傳遞給prepareForSegue:sender參數值是被輕觸的UITableViewCell。您可以使用UITableView方法indexPathForCell在表格中獲取單元格的indexPath,然後使用它來查找favoriteItems數組中的數據。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{  
    UITableViewCell *cell = (UITableViewCell*)sender; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    Favorite *favorite = [self.favoriteItems objectAtIndex:indexPath.row]; 

    stopString = favorite.title; 
    routeString = favorite.subtitle; 

    UIViewController *destination = segue.destinationViewController; 
    if ([destination respondsToSelector:@selector(setDelegate:)]) 
    { 
     [destination setValue:self forKey:@"delegate"]; 
    } 
    if ([destination respondsToSelector:@selector(setSelection:)]) 
    { 
     NSString *route = routeString; 
     NSDictionary *selection1 = [NSDictionary dictionaryWithObjectsAndKeys:route, @"route", stopString, @"stop", nil]; 
     [destination setValue:selection1 forKey:@"selection"]; 
    } 
} 
+0

是的!非常感謝你這完全解決了我的問題! – 2012-08-17 04:17:15

+0

很好的答案。正在尋找這個全部SO – 2014-03-02 08:16:03

0

確保您沒有將segue直接連接到tableView CELL的下一個視圖控制器。連接到整個UITableViewController/UIViewController(無論你使用什麼)並給出一個名字,比如說「segueNameInStoryboard」。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Favorite *favorite = [self.favoriteItems objectAtIndex:indexPath.row]; 

    stopString = favorite.title; 
    routeString = favorite.subtitle; 

    /* add this line */ 
    [self performSegueWithIdentifier:@"segueNameInStoryboard" sender:self]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([segue.identifier isEqualToString:@"segueNameInStoryboard"]) 
    {  
     UIViewController *nextViewController = segue.destinationViewController; 
     nextViewController.delegate = self; 

     NSDictionary *selection1 = [NSDictionary dictionaryWithObjectsAndKeys:routeString, @"route", stopString, @"stop", nil]; 
     nextViewController.selection = selection1; 
    } 
} 
+0

我也試過這個答案,它也有效! – 2012-08-17 04:18:42

相關問題