Objective-C (iOS): ダイアログの表示

iOSシミュレータのスクリーンショット 2013.01.03 16.07.01

Objective-C: UIAlertViewController.h

#import <UIKit/UIKit.h>

@interface UIAlertViewController : UIViewController <UIAlertViewDelegate>

@end

Objective-C: UIAlertViewController.m

#import "UIAlertViewController.h"

@interface UIAlertViewController ()

@end

@implementation UIAlertViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(20,20,150, 50);
    [button setTitle:@"Show Alert" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(showAlert) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)showAlert
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert Test" message:@"Hello World" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Other", @"OK", nil];
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            NSLog(@"Cancel was clicked.");
            break;
        case 1:
            NSLog(@"Other was clicked.");
            break;
        case 2:
            NSLog(@"OK was clicked.");
            break;
            
        default:
            break;
    }
}

@end