UIActionSheetを使用してアクションシートを表示するサンプルを以下に作成しました。
ボタンをタップでアクションシートが呼ばれ、アクションシート内のボタンをタップするとインデックス番号がラベルに表示されます。
サンプルダウンロード
ソースコード
ViewController.h
1
2
3
4
5
6
7
8
9
| #import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
- (IBAction)tapButton:(id)sender;
@end
|
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| #import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//ラベルのテキストを非表示にする
self.myLabel.text = NULL;
}
- (IBAction)tapButton:(id)sender {
//ボタンをタップでアクションシート呼び出し
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Title"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Caution"
otherButtonTitles:@"One",@"Two", nil];
//アクションシートを Viewに追加
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
//アクションシートのボタンが押された時に、ボタンのインデックス番号を返す
self.myLabel.text = [NSString stringWithFormat:@"ボタン=%d", buttonIndex];
}
@end
|
お薦めの参考書
Swiftではじめる iPhoneアプリ開発の教科書 Swift 2 & Xcode 7対応
この参考書の通りに実践サンプルを作成することで、確実に力を養うことができます。サンプルコードもサイトからダウンロード出来るようになっており、特に広告の貼り方などが参考になりました。Swiftで何が出来るかを知ることができ、入門用にお薦めの1冊です。