トップ «前の日記(2012-03-12) 最新 次の日記(2012-03-14)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|

2012-03-13 [iOS]ユーザに選択させる(UIActionSheet)

前回のアラートは、ユーザに情報を伝える為のもの。今回のアクション・シートはユーザに選択させる為のものだ。

ビューコントローラにUIActionSheetDelegateプロトコルを設定する。

@interface ViewController : UIViewController <UIActionSheetDelegate>
@end

アクションシートを表示させる。

@implementation ViewController
 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIActionSheet   *actionSheet = [[UIActionSheet alloc] initWithTitle:@"My Action Sheet"
                                                               delegate:self
                                                      cancelButtonTitle:@"Cancel"
                                                 destructiveButtonTitle:@"destructive button"
                                                      otherButtonTitles:@"Button 1",
                                                                        @"Button 2", 
                                                                        nil];
    [actionSheet showInView:self.view];
}
 
@end

アラートと同様にアクション・シートもデリゲートのメソッドでボタン押下に対応する。

@implementation ViewController
  
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%s index(%d)", __func__, (int)buttonIndex);
}
 
- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
    NSLog(@"%s", __func__);
}
 
@end

実行。

アクションシート

アクション・シートでは、追加するボタンの個数は可変だ。画面に収まりきれなく個数を指定した場合は、どうなるのだろうか?

@implementation ViewController
 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIActionSheet   *actionSheet = [[UIActionSheet alloc] initWithTitle:@"My Action Sheet"
                                                               delegate:self
                                                      cancelButtonTitle:@"Cancel"
                                                 destructiveButtonTitle:@"destructive button"
                                                      otherButtonTitles:@"Button 1",
                                                                        @"Button 2",
                                                                        @"Button 3",
                                                                        @"Button 4",
                                                                        @"Button 5",
                                                                        @"Button 6", 
                                                                        nil];
    [actionSheet showInView:self.view];
}
 
@end
ピッカー

なんと、ボタンの部分がピッカーになっている!

第50回関東Cocoa勉強会で@saeki24hさんが、自身が発見されたアクション・シートのバグを発表されていましたが、その時はボタンの個数が増えてピッカーになった際に、cancelボタンとdestructiveボタンの順番が変わって、その際、インデックスがおかしくなっていたが、iOS 5.1で修正されたのか、ボタンの順番が変わらず、その為か、インデックスは正しい値のようだ。


トップ «前の日記(2012-03-12) 最新 次の日記(2012-03-14)» 編集