I am performing a segue to a view controller that requires an initialisation process. At present the initialisation happens in ViewDidLoad of the target view controller. However the initialisation is fairly lengthy and I would like to show a spinner while it is happening.
If I create a UIActivityIndicatorView within ViewDidLoad and run the initialisation on another thread, of course ViewDidLoad exits and the rest of the loading process happens - in particular shouldAutorotate is called, and this contains code that assumes the initialisation process has occurred. (Even if it didn't, I do not want to show the target view before it has been initialised.)
The answer seems to be to initialise the target view controller before calling the segue. However I can't do that in prepareForSegue in the calling view controller, for the same reason - it exits and the segue is called before the initialisation has happened.
So I seem to need to instantiate the target controller, initialise it and then perform the segue with the initialised controller as the destination. My problem is that I don't know how to do that. The only possible way I have come across is to subclass UIStoryboardSegue and put the initialisation in the init for the subclass. Then I presume I call
UIStoryboardSegue * segue = [[SubclassedSegue alloc]initWithIdentifier:@"??what should this be??" source:self destination:targetViewContoller];
[segue perform]; // which just calls [super perform];
from the source view controller. Is this correct? Can anyone please show me some example code that uses this process - or preferably a simpler way that I haven't thought of? I can't help thinking there must be an easier way to show a spinner.
Thank you for your help.
Segues should be subclassed only when you need to show custom animation/transition during the segue.
In usual scenario, you would want to do this:
__ block Destination *destinationVC = [self.storyboard instantiateViewControllerWithIdentifier:@"destination "];
//START BUSY CURSOR HERE
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
// init whatever you want for destinationVC HERE.
dispatch_async(dispatch_get_main_queue(),
^{
//STOP BUSY CURSOR
//PERFORM UI UPDATE HERE
[self presentViewController:destinationVC animated:YES completion:nil];
});