Search Results

Filtered by:UIAlertView

Site Search

Search Articles.

iPhone: Modal Dialogs

Sunday 11 Apr 2010 19:50

Have you ever wondered how to create modal dialogs on the iPhone? Dreamed of simply calling a confirmation dialog and getting back the user input without the need for delegates and messy code.

Read more about modal dialogs here.

Tags: Modal Dialogs UIAlertView iPhone programming

iPhone: Modal Dialogs

Sunday 11 Apr 2010 19:48

If like me you come from a Windows programmer background then you might be surprised at the lack of support for modal dialogs in the iPhone OS. Apple provide you with a UIAlertView which is great for on screen prompts and confirmations, but using them can be quite tedious and require a lot more code than is often neccessary.

This means you can't have code like this

result = CreateModalDialog(NSTitle, NSMsg, @"Ok", @"Cancel");

Instead you have to provide delegates which are called once the user selects an option. In other words your code does not stop and wait for a return value. Whilst this has its benefits in terms of multitasking, your code can get quite messy as you chain these delegates together.

There is another way which involves creating a custom UIAlertView class and a run loop. The code should be self explanatory.

UIAlertView *alert;   
@interface MyUIAlertViewDelegate : NSObject <UIActionSheetDelegate, UIAlertViewDelegate> 
  int result;


@implementation MyUIAlertViewDelegate

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
	result = buttonIndex;

	return result;


int CreateModalDialog(NSString *title,
                      NSString *msg, 
                      NSString *ok, 
                      NSString *cancel)
	// Create an instance of a custom UIAlertViewDelegate that we use to capture
	// the events generated by the UIAlertView
	MyUIAlertViewDelegate *lpDelegate = [[MyUIAlertViewDelegate alloc] init];

	// Construct and "show" the UIAlertView (message, title, cancel, ok are all
	// NSString values created earlier in your code...)
	UIAlertView *lpAlertView = [[UIAlertView alloc] 
otherButtonTitles:ok, nil]; [lpAlertView show]; // By the time this loop terminates, our delegate will have been
// called and we can get the result from the delegate (i.e. what button was pressed...) while ((!lpAlertView.hidden) && (lpAlertView.superview!=nil)) { [[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode]; Sleep(10); } // Grab the result from our delegate (via a custom property) int nResult = [lpDelegate getResult]; // Tidy up! [lpAlertView release]; [lpDelegate release]; NSLog(@"Result: %d", nResult); return nResult; } @end;

Tags: Modal Dialogs UIAlertView iPhone programming

iPhone: Creating a custom UIAlertView Busy Dialog

Wednesday 31 Mar 2010 21:34

Have you ever wanted to create a "Please Wait" dialog on the iPhone without having to implement a custom UIView?  In this article I explain how to cusomize a UIAlertView dialog in order to achieve the same effect.

Click here to view the article.

Tags: UIAlertView iPhone Custom

iPhone: Creating a custom UIAlertView Busy Dialog

Wednesday 31 Mar 2010 17:59

One of the major problems facing iPhone developers, especially those coming from a Windows environment  is the lack of dialogs. We use dialogs when we require user input or need to display feedback.  I recently needed to display a 'Please Wait' dialog whilst downloading files from a web server, and found a way to do this using a UIAlertView.

The iPhone's UIAlertView generally displays an on screen message allowing the user to select one or more options as in the image below.

In the case of a "Please Wait" dialog we don't require any buttons and need to insert a UIActivity indicator inside the dialog. Generally speaking you can embed most UI elements inside a UIAlertView, but to do this you need to expand the dialog, which you can do by inserting a series of returns in the message body. In my case removing the buttons gives me ample room to insert the activity indicator.

UIAlertView *WaitPrompt()
  UIAlertView *alert = [[[UIAlertView alloc] 
    initWithTitle:@"Contacting Server\nPlease Wait..." 
    message:nil delegate:nil cancelButtonTitle:nil
    otherButtonTitles: nil] autorelease];
  [alert show];

  UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50); [indicator startAnimating]; [alert addSubview:indicator]; [indicator release]; // 250ms pause for the dialog to become active... int n=0; while(n < 250) { [[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode]; Sleep(1); n++; } return alert; }

We can call the above routine to create our wait dialog, but how do we dismiss the dialog once we have completed our task?

void DismissWaitPrompt(UIAlertView *alert)
	[alert dismissWithClickedButtonIndex:0 animated:YES];

Please pay special attention to the while() loop at the end of the WaitPrompt() function. This section of code executes the main run loop for 250ms which is enough time for the OS to animate the UIAlertView onto the display. If you omit this section of code the chances are the dialog would not display until your processing is complete.

Tags: iPhone UIAlertView Custom programming


Sites of Interest