Search Results

Filtered by:programming

Site Search

Search Articles.



iPhone: Modal Dialogs

Sunday 11 Apr 2010 20: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 20: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");
switch(result)
{
   ....
}

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.

ModalDialog.h
UIAlertView *alert;   
    
@interface MyUIAlertViewDelegate : NSObject <UIActionSheetDelegate, UIAlertViewDelegate> 
{
  int result;
}

-(int)getResult;

@end;

ModalDialog.mm
@implementation MyUIAlertViewDelegate

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

-(int)getResult
{
	return result;
}

@end;

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] 
initWithTitle:title
message:msg
delegate:lpDelegate
cancelButtonTitle:cancel
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 18: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]; indicator.center = 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

Programming Tips

Friday 12 Mar 2010 21:43

In my real life I've been a software engineer and C++ programmer for the past 20+ years! (A fact I was only reminded of today, and one that I don't like to contemplate for too long).

I've worked on many platforms including Windows, Windows Mobile, iPhone, DS, DSi and many more. I know from experience how difficult it can be to find good information and tutorials. I am not quite sure how this section is going to pan out, and if I will be posting tutorials or code snippits.

Time will tell...

Tags: programming iPhone Windows Mobile

Links

Sites of Interest