▼iPhoneプログラミングメモ▼

ボタンとアラートとアクションシート


ボタンとアラートとアクションシートを利用するプログラムを作成する。


画像の準備
Resourcesに追加。

sample.png


ソースコードの記述
ButtonEx.h
#import <UIKit/UIKit.h>

//ButtonExの宣言
@interface ButtonEx : UIViewController <
    UIAlertViewDelegate,UIActionSheetDelegate> {
}
@end

ButtonEx.m
#import "ButtonEx.h"

#define BTN_ALERT   0
#define BTN_YESNO   1
#define BTN_SHEET   2
#define BTN_IMAGE   3

//ButtonExの実装
@implementation ButtonEx

//アラートの表示
- (void)showAlert:(NSString*)title text:(NSString*)text {
    UIAlertView* alert=[[[UIAlertView alloc] 
        initWithTitle:title message:text delegate:nil 
        cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show];
}

//YES/Noダイアログの表示
- (void)showYesNoDialog:(NSString*)title text:(NSString*)text {
    UIAlertView* alert=[[[UIAlertView alloc] 
        initWithTitle:title message:text
        delegate:self cancelButtonTitle:@"No" 
        otherButtonTitles:@"Yes", nil] autorelease];
    [alert show];
}

//テキストボタンの生成
- (UIButton*)makeButton:(CGRect)rect text:(NSString*)text tag:(int)tag {
    //テキストボタンの生成
    UIButton* button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:rect];
    [button setTitle:text forState:UIControlStateNormal];
    [button setTag:tag];
    
    //イベントリスナーの指定
    [button addTarget:self action:@selector(clickButton:) 
        forControlEvents:UIControlEventTouchUpInside];
    return button;
}

//イメージボタンの生成
- (UIButton*)makeButton:(CGRect)rect image:(UIImage*)image tag:(int)tag {
    UIButton* button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:rect];
    [button setImage:image forState:UIControlStateNormal];
    [button setTag:tag];
    [button addTarget:self action:@selector(clickButton:) 
        forControlEvents:UIControlEventTouchUpInside];
    return button;
}

//初期化
- (void)viewDidLoad {
    [super viewDidLoad];

    //アラート表示ボタンの生成
    UIButton* btnAlert=[self makeButton:CGRectMake(0,0,200,40) 
        text:@"アラート表示" tag:BTN_ALERT];
    [btnAlert addTarget:self action:@selector(clickButton:) 
        forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnAlert];

    //YES/NOダイアログの生成
    UIButton* btnYesNo=[self makeButton:CGRectMake(0,50,200,40) 
        text:@"YES/NOダイアログ表示" tag:BTN_YESNO];
    [self.view addSubview:btnYesNo];

    //アクションシート表示ボタンの生成
    UIButton* btnSheet=[self makeButton:CGRectMake(0,100,200,40) 
        text:@"アクションシート表示" tag:BTN_SHEET];
    [self.view addSubview:btnSheet];

    //イメージボタンの生成
    UIButton* btnImage=[self makeButton:CGRectMake(0,150,114,114) 
        image:[UIImage imageNamed:@"sample.png"] tag:BTN_IMAGE];
    [self.view addSubview:btnImage];
}

//ボタンクリック時に呼ばれる
- (IBAction)clickButton:(UIButton*)sender {
    if (sender.tag==BTN_ALERT) {
        //アラートの表示
        [self showAlert:@"" text:@"アラート表示ボタンを押した"];
    } else if (sender.tag==BTN_YESNO) {
        //YES/NOダイアログの表示
        [self showYesNoDialog:@"" text:@"YES/NOダイアログ表示ボタンを押した"];
    } else if (sender.tag==BTN_SHEET) {
        //アクションシートの表示
        UIActionSheet* sheet=[[[UIActionSheet alloc] 
            initWithTitle:@"アクションシートの表示" delegate:self 
            cancelButtonTitle:@"キャンセル" 
            destructiveButtonTitle:@"おわり" 
            otherButtonTitles:@"項目0",@"項目1",@"項目2",nil]
            autorelease];
        [sheet showInView:self.view];
    } else if (sender.tag==BTN_IMAGE) {
        //アラートの表示
        [self showAlert:@"" text:@"イメージボタンを押した"];
    }
}

//アラートボタンクリック時に呼ばれる
- (void)alertView:(UIAlertView*)alertView 
    didDismissWithButtonIndex:(NSInteger)index {
    NSString* text=[NSString stringWithFormat:@"項目%dをクリック",index];    
    [self showAlert:@"" text:text];
}

//アクションシートボタンクリック時に呼ばれる
- (void)actionSheet:(UIActionSheet*)actionSheet 
    didDismissWithButtonIndex:(NSInteger)index {
    NSString* text=[NSString stringWithFormat:@"項目%dをクリック",index];    
    [self showAlert:@"" text:text];
}

//画面を端末の向きにあわせて回転
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)orientation {
    return YES;
}
@end


−戻る−