Here are a few ways of showing alerts in you iPhone application using UIActionSheet and UIAlertView controls. You can also download the test XCode project where this sample code was generated.


Simple Action Sheet

    // open a dialog with just an OK button
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title"
                                                delegate:nil 
                                                cancelButtonTitle:nil 
                                                destructiveButtonTitle:@"OK"
                                                otherButtonTitles:nil];
 
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
    [actionSheet showInView:self.view];
    [actionSheet release];

iphone alert xcode

OK – Cancel Action Sheet

    // open a dialog with OK and Cancel button
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title"
                                                delegate:nil 
                                                cancelButtonTitle:@"Cancel"
                                                destructiveButtonTitle:@"OK"
                                                otherButtonTitles:nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
    [actionSheet showInView:self.view];
    [actionSheet release];

iphone ok cancel actionsheet




Simple Alert

    // open an alert with just an OK button
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                        message:@"My message" 
                                        delegate:self 
                                        cancelButtonTitle:@"Ok"
                                        otherButtonTitles: nil];
    [alert show];
    [alert release];

iphone alert xcode

OK – Cancel Alert

    // open a alert with an OK and cancel button
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                        message:@"My message" delegate:self 
                                        cancelButtonTitle:@"Cancel"
                                        otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];

alert iphone ok cancel


Leave a Reply


+ one = 8