0

Similar to this question: Adding subview, gets delayed? But I don't think you can pushViewController in a separate thread so is this really impossible?

Here is what I'm trying to do:

I have a TableView and when a cell is pressed, I want to call

[self.view addSubview:LoadingView]

to display an overlay with a spinner. Then I call

self.navigationController.navigationBar.hidden = NO;
[self.navigationController pushViewController:newGameViewController animated:YES];

However, the subview only displays for a split second (~1-4 seconds after the cell selection occurs while it waits for the new viewcontroller to initialize).

Is there any way to get some sort of loading indicator to occur at the instant the cell is selected?

Community
  • 1
  • 1
Evan Layman
  • 3,691
  • 9
  • 31
  • 48
  • I'm not sure if I understand you right. Do you wonna delay the loading of the new view controller an just lengthen the period the spinner is display? – Bernd Rabe Aug 12 '12 at 16:20
  • That could work too.. The issue is the new view controller accesses data from my server which takes a few seconds - leaving the user at an unresponsive, non-updated view. I want to display the loader at the instant they press the button so they know something is happening ;) – Evan Layman Aug 12 '12 at 16:23

3 Answers3

1

Okay. What about this. In your didSelectRowAtIndexPath start your spinner (via addSubview ...) and start loading your stuff from the server. If that's finished remove the spinner an push your new view controller onto the stack. Make sure the user can't touch any other cell during that time. By the way. From a users perspective I'd find it mor intuitive if the new controller is loaded immediately and displays some waiting message.

Bernd Rabe
  • 790
  • 6
  • 23
1

Or the other way around: The newGameViewController displays the spinner and starts loading the data from the server in a background thread. When the data is complete, remove the spinner and display the data. That way the user could even go back if she doesn't want to wait.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Actually.. I realize I decide which view controller to push via my appDelegate which cannot add the subview to the view (I am an ios/objective c noobie so forgive me if I'm wrong in saying this) – Evan Layman Aug 12 '12 at 16:39
1

You do not need to add code before pushing newGameViewController.

Inside viewDidLoad of newGameViewController, write the code of displaying spinner. To get the updated UI, just insert a delay before calling a web API.

inside GameViewController.m

-(void) viewDidLoad
{
    [self.view addSubview:LoadingView];
    [self performSelector:@selector(callWebAPI) afterDelay:0.1];
}

-(void) callWebAPI
{
    //Handle network activity here..
}
Apurv
  • 17,116
  • 8
  • 51
  • 67
  • So we had the same idea :-) But I would not use `performSelector:withObject:afterDelay:` because the selector will be executed on the main thread and block the UI while running. – Martin R Aug 12 '12 at 17:48