skip to Main Content

I’m trying to pass the storeId to another viewController using segue, however, there seems to be a delay when passing data? Data is "nil" at first, I have to tap it again in order it to pass the correct data.

Here is my code


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
    self.passStoreData = self.storeList [indexPath.row];
    self.passStoreId = [self.storeList [indexPath.row] valueForKey:@"storeId"];
    self.passStoreName = [self.storeList [indexPath.row] valueForKey:@"storeName"];
    NSString *store = [NSString stringWithFormat:@"%@ : %@", self.passStoreId, self.passStoreName];
    
    [self.storeButton setTitle:[NSString stringWithFormat:@"%@", store] forState:UIControlStateNormal];
    storeTable.hidden = YES;
    
    [self performSegueWithIdentifier:@"showOrder" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    OrderListViewController *orderListVc = [segue destinationViewController];

    orderListVc.theStoreId = self.passStoreData;
    
}

What can I do to avoid the delay in passing the data using segue? Thank you.

2

Answers


  1. Chosen as BEST ANSWER

    After trying and researching. I found the answer to this.

    I used dispatch_async(dispatch_get_main_queue() to run it on main thread and the passing of data through segue was not delayed. Thank you


  2. By the time you call [segue destinationViewController], the OrderListViewController is loaded and ready to use. So any code in viewDidLoad will already have been called, way before you set theStoreId.

    Move your view setup code to viewWillAppear: and it should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search