xyk blog

最近は iOS 開発の記事が多めです。

UIActionSheet を試す

環境:iOS SDK 7.1

1. UIViewControllerを継承したSampleViewControllerクラスを作成する
SampleViewController.h
SampleViewController.m
2. AppDelegateクラスを修正し、SampleViewControllerに遷移するようにする

AppDelegate.m

#import "AppDelegate.h"
#import "SampleViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];

    self.window.rootViewController = [[SampleViewController alloc] init];
    return YES;
}

// snip
3. ヘッダファイルにUIActionSheetDelegateプロトコルを追加する

SampleViewController.h

@interface SampleViewController : UIViewController <UIActionSheetDelegate>
4. 画面最下部に空のツールバーを表示する

SampleViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    // ツールバーを作成(画面最下部に空のツールバーを表示する)
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 44, 320, 44)];
    // ツールバーを親Viewに追加
    [self.view addSubview:toolBar];
}
5. ツールバーにボタンを2つ表示する

SampleViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    // ツールバーを作成(画面最下部に空のツールバーを表示する)
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 44, 320, 44)];
    // ツールバーを親Viewに追加
    [self.view addSubview:toolBar];

    // ツールバーに2つのボタンを追加
    UIBarButtonItem *btn1 = [[UIBarButtonItem alloc] initWithTitle:@"テスト1"
                                                            style:UIBarButtonItemStyleBordered
                                                           target:self
                                                           action:@selector(button1DidPush)];

    UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithTitle:@"テスト2"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:self
                                                            action:@selector(button2DidPush)];

    toolBar.items = @[btn1, btn2];
}

シミュレータで確認。

f:id:xyk:20140909172837p:plain

6. ボタンタップ時にアクションシートを表示する

「テスト1」と「テスト2」で違う書き方をしてみた。

SampleViewController.m

// 「テスト1」ボタンタップ時にアクションシートを作成する
- (void)button1DidPush
{
    UIActionSheet *as = [[UIActionSheet alloc] init];
    as.delegate = self;
    // as.title = @"選択してください"; // タイトルは省略可
    [as addButtonWithTitle:@"実行"];
    [as addButtonWithTitle:@"やめる"];
    [as addButtonWithTitle:@"キャンセル"];
    as.destructiveButtonIndex = 0; // 文字を赤く目立たせる
    as.cancelButtonIndex = 2; // キャンセルボタン
    [as showInView:self.view];
}

// 「テスト2」ボタンタップ時にアクションシートを作成する
- (void)button2DidPush
{
    UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@"タイトル"
                                                    delegate:self
                                           cancelButtonTitle:@"キャンセル"
                                      destructiveButtonTitle:@"注意"
                                           otherButtonTitles:@"処理1", @"処理2", @"処理3", nil];
    [as showInView:self.view];
}

「テスト1」ボタンタップ時

f:id:xyk:20140909172838p:plain

「テスト2」ボタンタップ時

f:id:xyk:20140909172839p:plain

7. アクションシート選択時の処理を実装する

actionSheet:clickedButtonAtIndex:メソッドを追加して実装する

// いずれかのアクションが選択されたタイミングで呼ばれる
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"buttonIndex: %d", buttonIndex);
}