Posts Tagged ‘uialertview’

iPhone OS SDK – UIAlert & UIAlert Action on button click

March 18th, 2010 iPhone SDK Tips

Using a UIAlertView in apps is very common and surprisingly complex. Its 2 lines, but comparing to VBasic its complicated. This is done by:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Welcome to Savings Goal” message:@”Please press the red button to setup your savings goal and current amount saved.” delegate:self cancelButtonTitle:nil otherButtonTitles:@”Ok Lets Save!”,nil];
[alert show];

These two lines I keep using over and over again. They form a very common copy and paste in my projects.
More interesting is performing an action on a button click in the UIAlertView. Lets say you have 2 buttons (buttonIndex 0 and 1). use the following method to execute code on that button press.

- (void)alertView: (UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex
{
if (buttonIndex==0) {
NSLog(@”In AlertView Delegate”);
//start a timer
timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerVoid) userInfo:nil repeats:YES];
}

}

This will start a timer (NSTimer *timer), when the first button is pressed in an UIAlert. For UIAlerts with multiple buttons, you should use the int buttonIndex to detect which button was pressed.

Thats all for today, enjoy.

iPhone Development – Learning to use a date spinner (date picker)

January 21st, 2010 iPhone SDK Tips

The imfamous iPhone date reel/spinner/picker is easily implemented into your own custom iPhone app. The Class Reference for the UIDatePicker is available here.

I would like to share a simple project I made to help understand the UIDate Picker. This was based on the iPhone Developers Cookbook Example.

The project is a simple date spinner which once it sets on a date sends a UIAlert of the date. It outlines well the UIDate Picker and an UIAlert. The project is extremely simple, but hopefully someone will find it useful. (more…)