2

こんにちは、私はしばらく交通機関アプリに取り組んできましたが、しばらくこの問題に悩まされています。

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にかなり慣れていないので、友人が私に言ったセグエを使用していますが、それが問題かもしれません. セグエは次のようになります。

- (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 でセグエを実行した後、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 が両方とも null であるため、情報が送受信されませんでした。しかし、テーブルに戻って別のセルをクリックすると、routeName と stopName には、最初にセルをクリックしたときに送信されたはずの情報が入力されています。このプロセスを続行すると、現在ではなく、以前にタップされたセルの情報が引き続き送信されます。

つまり、基本的に情報は送信されますが、セグエを 2 回通過した後にのみ送信されます。明らかに、最初に情報を送信して受信することを望んでいますが、それは遅れており、気が狂います。この問題を解決しようと何日もインターネットを検索してきたので、誰かが私に助けてくれることに感謝します。

4

2 に答える 2

6

prepareForSegue:は前に呼び出されていdidSelectRowAtIndexPath:ます。これが、表示される値が常に遅れている理由です。

より良い解決策は、メソッドで stopString と routeString の値を取得することですprepareForSegue:(まったく使用didSelectRowForIndexPath:しないでください)。これを行うための鍵は、sender渡されるパラメーター値prepareForSegue:がタップされた 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"];
    }
}
于 2012-08-16T06:14:06.823 に答える
0

次のView Controllerへのセグエを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;
    }
}
于 2012-08-16T08:19:22.287 に答える