when a user taps on an image in the iPhone App page, I want to first show a custom pop-up (which is a user message saying “your request is being processed”), “before” beginning the processing of the image-touch. But, somehow the processing begins (without even displaying the pop-up, though I have written the code to display the pop-up ‘before’ the processing code) & almost ends, & only then the pop-up is displayed. Can you please help?
This is the code snippet :
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
int emotionChosen = -1;
[self showUserNote:msg];
...
...
// This then calls a method to process the touch event
...
...
}
-(void) showUserNote:(NSString *)note {
if ((self.viewCtrlrUserNote == nil) || (self.viewCtrlrUserNote == NULL)) {
self.viewCtrlrUserNote = (ViewController_NoteToUser *) [((com_AppDelegate *) [[UIApplication sharedApplication] delegate]).storyboard instantiateViewControllerWithIdentifier:kNameViewCtrlrUserNote];
}
[self.viewCtrlrUserNote.view setFrame:CGRectMake(70.0,150.0,170.0,170.0)];
[self.viewCtrlrUserNote initializeViewWithNote:note];
[self.view addSubview:self.viewCtrlrUserNote.view];
[self.view bringSubviewToFront:self.viewCtrlrUserNote.view];
}
Though am making the call to showUserNote (which should display the pop-up/dialogbox) “first”, “and then only” am calling the method which processes the touch. But the method to process the touch begins execution first & the dialog box appears only near the end of the processing-method, & so it destroys the very purpose!
My Dev Environment : XCode 4.3.3, iPad, iOS 5
How do I “mandate” it that iOS “first” displays the user dialog “before” beginning to process the touch event?
Drawing and UI commands are generally not synchronous in CocoaTouch (or indeed, in any modern graphics API that uses 3D acceleration and compositing). When you do all the work in
-showUserNote:you are queuing up commands to the view subsystem to do all of those things the next time it processes events, not actually doing them.The standard pattern to achieve what you want is to use delegation have it the view callback into the delegate once it has drawn. Apple uses this pattern in the UIAlert class, what you should do is get your code working using that instead of your custom view, then modify your custom view to have a delegate and call back into it once it has displayed.
I highly recommend you read up on runloops so you can understand event processing and things like deferred processing.