True modal dialog

Is any way exist to make true modal dialog on Iphone? I Tried something like this:

UIAlertView* view = [[UIAlertView alloc] initWithTitle: @"Alert" message: @"Text" ....];
[view show];
[view release];
//my code


When "[view show];" is called and Alert is appeared next line in my code is executed. It isn't what I want... I need show dialog and return to next code execution only when dialog will be closed.

MacBook Pro, Mac OS X (10.5.7)

Posted on May 29, 2009 1:03 PM

Reply
20 replies

Jun 7, 2009 5:53 PM in response to xnav

I find this to be a very cumbersome programming design.

For one thing it makes it quite hard to port code from almost every other UI toolkit that does have true blocking alerts.

I also find it quite cumbersome when your code has several UIAlerts within the same class and you want the class to serve as the delegate (to keep related code together). Then you have to set flags or store the different UIAlert in instance variables in order to know what to do in the alertView:clickedButtonAtIndex: method.

I keep thinking I must be missing something and it can't really be this bad. Why did they do this?

Jun 8, 2009 5:46 AM in response to xnav

The block is on the application level, not the system level. From the user's point of view the behavior of Apple's design and a true blocking dialog is the same. The application can't do anything else until the user responds to the alert. However, from a programing point of view there is a world of difference and can't see any reason Apple's solution would be preferred.

Jun 6, 2009 11:50 AM in response to xnav

But App don't suspend after show:

UIAlertView* view = ...;
[view show];
NSLog(@"!!!");


In this example I'll see "!!!" in log but I don't want to see it before user didn't click on button in my dialog... Is any way exist to do what I want in Cocoa Touch (There is runModalWindow method exist on NSApplication class in Cocoa)

Jun 8, 2009 7:01 AM in response to btschumy

btschumy wrote:
It wasn't code ported from another mobile platform, but from a desktop program.


That part was already clear, thanks. Sorry, the question was, what mobile platform(s) have you previously ported any code --> to (not from)...?

I take it that prior to this episode, you have no experience working with mobile platforms/apps, etc.

Jun 8, 2009 7:26 AM in response to btschumy

btschumy wrote:
Why would that be relevant to the discussion?


Because a desktop computer and a mobile phone represent two different environments, for starters.

Casually assuming they are dutifully similar might present a hurdle towards otherwise fluid development.

Have you ever had to answer an incoming phone call on a desktop computer while a modal was displayed? Or perhaps had the opportunity to use a desktop computer to dial 911 when all users were logged out/screen locked?

Jun 8, 2009 7:43 AM in response to K T

As I said previously in a response to someone else, from the user's perspective there would be no difference between a true application modal dialog and what Apple has done. In either case the user can't do anything else with the application until responding to the dialog. The only difference (as far as I can tell) is that Apple's model is a bigger pain for the programmer.

If you can explain Apple's logic I would truly appreciate it. It is a mystery to me. The only thing I can guess is that they didn't know how to set up a secondary event loop to handle the modal dialog while the primary even thread was blocked. However, other toolkits seems to b able to deal with this (including NeXTSTEP).

Jun 8, 2009 8:27 AM in response to xnav

I'll repeat what I said above:

"I also find it quite cumbersome when your code has several UIAlerts within the same class and you want the class to serve as the delegate (to keep related code together). Then you have to set flags or store the different UIAlert in instance variables in order to know what to do in the alertView:clickedButtonAtIndex: method."

I find this spreading of the code logic needlessly complex.

With respect to timers or other asynchronous notifications, this is all quite doable. I've been working with Java/Swing for quite some time and they have solved the issue. You just need to block user events in other windows when the modal dialog is up. The code can still respond to timers and programmatic events on the modal event thread.

Jul 1, 2009 12:16 AM in response to pKiR

I had a similar problem.

Let's say I wanted to execute a loop of things that must be done sequentially

for (int i=0; i< 100; i++){
if (i==50){
<do some stuff and show alert>
}
}

Coming from the Java world, I was trying to figure out how to lock/unlock or synchronize the alert on a GUI/event thread. The closest that we get to that is really performSelectorOnMainThread: which manages to lock the GUI up where we expect it to do something. Creating a @synchronized block, using an NSLock, or NSConditionLock are also useless for this problem. But since I have to look into these low-level details, it just leaves a bad taste in my mouth for how crude and/or lacking the API really is. Since the UIAlertView really is asynchronous, the loop continues on.

There are people out there who will assume you haven't read the manual, don't know what you're doing, or tell you "its not the Apple way", but really at the end of the day we all just want to get our little bit of code working the way we expect it to, and your expectations are not unreasonable. The API for doing so is sometimes unwieldy, so you can (and will) write a lot of wrappers for methods that you will reuse in a "default" way.

Personally, I'd hope for a more elegant solution from Apple similar to the BlockingQueue approach in java.util.concurrent that you could push/pop operations and GUI and non-gui objects in a more sequential manner.

I understand your frustration and wanted to get this done myself, so I will explain what I did. I had to partition the loop that I wanted to do things in, where effectively the modal dialog (alert) became the logical "pivot".

I broke the original problem up into

for (int i=0; i<51; i++){
// Do stuff
}

// Show alert..

for (int i=51; i< 100){
// Do more stuff
}

I implemented a protocol that I made up called Command, that implement instance methods executeBeforeAlert for the first half of the problem, and executeAfterAlert for the second half of the problem.

I created another class called LoopAlertView that takes a Command, and calls
[command executeAfterAlert] in the alertView:clickedButtonAtIndex.

This gave me effectively the behavior that I wanted -- blocking on an operation until the dialog is dismissed.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

True modal dialog

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.