①「ViewController.h」に
[php]
//————————————————-//
//フレームワークをインポート
//————————————————-//
#import <UIKit/UIKit.h>
//————————————————-//
//クラスとインスタンス変数の宣言
//————————————————-//
@interface ViewController : UIViewController <UIAlertViewDelegate,UIActionSheetDelegate>
//————————————————-//
//アクセサメソッドの宣言を
//————————————————-//
//————————————————-//
//オリジナルメソッドの宣言
//————————————————-//
– (void)configureView;
– (void)alertOKCancel;
– (void)actionOKCancel;
//————————————————-//
//ヘッダーファイル
//————————————————-//
@end
[/php]
②ViewController.m
[php]
/————————————————-//
//ヘッダーファイルをインポート
//————————————————-//
#import "ViewController.h"
@interface ViewController ()
@end
//————————————————-//
//クラスとアクセサメソッドの実装
//————————————————-//
@implementation ViewController
//————————————————-//
// 処理
//————————————————-//
– (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// ビューの設定をまとめて行うメソッドを呼び出します。
[self configureView];
}
//————————————————-//
// ビュー[.hのオリジナルメソッド]
//————————————————-//
– (void)configureView
{
UIButton *customButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
customButton1.frame = CGRectMake(40.0, 100.0, 240.0, 40.0);
[customButton1 setTitle:@"アラート" forState:UIControlStateNormal];
[customButton1 addTarget:self action:@selector(alertOKCancel) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:customButton1];
UIButton *customButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
customButton2.frame = CGRectMake(40.0, 180.0, 240.0, 40.0);
[customButton2 setTitle:@"アクションシート" forState:UIControlStateNormal];
[customButton2 addTarget:self action:@selector(actionOKCancel) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:customButton2];
}
//————————————————-//
// [.hのオリジナルメソッド]
//————————————————-//
– (void)alertOKCancel
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"アラート"
message:@"よろしいですか?"
delegate:self
cancelButtonTitle:@"いいえ"
otherButtonTitles:@"はい", nil];
[alert show];
}
– (void)actionOKCancel
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"アクションシート"
delegate:self
cancelButtonTitle:@"いいえ"
destructiveButtonTitle:@"はい"
otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
}
//————————————————-//
// デリゲートメソッド
//————————————————-//
– (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
– (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"『%d番』のボタンが押されました!",buttonIndex);
}
– (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"『%d番』のボタンが押されました!",buttonIndex);
}
//————————————————-//
// メソッドファイルend
//————————————————-//
@end
[/php]