回答編集履歴

6

修正

2016/07/17 07:36

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
 
28
28
 
29
-
29
+ // UIBarButtonItemを押下時
30
30
 
31
31
  @IBAction func pushItem(sender: UIBarButtonItem) {
32
32
 
@@ -76,26 +76,96 @@
76
76
 
77
77
  //アクションシートにアクションボタンを追加
78
78
 
79
-
79
+ sheet.addAction(cancelAction)
80
80
 
81
81
  sheet.addAction(cameraAction)
82
82
 
83
83
  sheet.addAction(LibraryAction)
84
84
 
85
+
86
+
87
+ //アクションシートを表示
88
+
89
+ sheet.popoverPresentationController?.sourceView = view
90
+
91
+ sheet.popoverPresentationController?.barButtonItem = sender
92
+
93
+
94
+
95
+ self.presentViewController(sheet, animated: true, completion: nil)
96
+
97
+ }
98
+
99
+
100
+
101
+ // UIButtonを押下時
102
+
103
+ @IBAction func cameraTapped(sender: UIButton) {
104
+
105
+
106
+
107
+ //UIImagePickerControllerを使うための定数
108
+
109
+ let pickerController = UIImagePickerController()
110
+
111
+ //UIImagePickerControllerのデリゲートメソッドを使用する設定
112
+
113
+ pickerController.delegate = self
114
+
115
+ //UIAcionSheetを使うための定数を作成
116
+
117
+ let sheet = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
118
+
119
+ //ボタンタイトルをNSLocalizedStringに変更
120
+
121
+ let cancelString = NSLocalizedString("Cancel", comment: "キャンセル")
122
+
123
+ let cameraString = NSLocalizedString("Camera", comment: "カメラ")
124
+
125
+ let libraryString = NSLocalizedString("Library", comment: "ライブラリ")
126
+
127
+ //3つのアクションボタンの定数を作成
128
+
129
+ let cancelAction = UIAlertAction(title: cancelString, style: .Default, handler: {(action) -> Void in})
130
+
131
+ let cameraAction = UIAlertAction(title: cameraString, style: .Default, handler: {(action) -> Void in
132
+
133
+ pickerController.sourceType = .Camera
134
+
135
+ self.presentViewController(pickerController, animated: true, completion: nil)})
136
+
137
+ let LibraryAction = UIAlertAction(title: libraryString, style: .Default, handler: {(action) -> Void in
138
+
139
+ pickerController.sourceType = .PhotoLibrary
140
+
141
+ self.presentViewController(pickerController, animated: true, completion: nil)})
142
+
143
+
144
+
145
+
146
+
147
+ //アクションシートにアクションボタンを追加
148
+
85
149
  sheet.addAction(cancelAction)
86
150
 
151
+ sheet.addAction(cameraAction)
152
+
153
+ sheet.addAction(LibraryAction)
154
+
87
155
 
88
156
 
89
157
  //アクションシートを表示
90
158
 
91
159
  sheet.popoverPresentationController?.sourceView = view
92
160
 
93
- sheet.popoverPresentationController?.barButtonItem = sender
161
+ sheet.popoverPresentationController?.sourceRect = sender.frame
94
162
 
95
163
 
96
164
 
97
165
  self.presentViewController(sheet, animated: true, completion: nil)
98
166
 
167
+
168
+
99
169
  }
100
170
 
101
171
  }

5

修正

2016/07/17 07:36

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
 
14
14
 
15
- class ViewController: UIViewController {
15
+ class ViewController: UIViewController , UIImagePickerControllerDelegate, UINavigationControllerDelegate {
16
16
 
17
17
 
18
18
 
@@ -20,37 +20,81 @@
20
20
 
21
21
  super.viewDidLoad()
22
22
 
23
+
24
+
23
25
  }
24
26
 
25
27
 
26
28
 
27
- // UIButtonを押下時のアクションを設定
29
+
28
30
 
29
- @IBAction func showActionSheet(sender: UIButton) {
31
+ @IBAction func pushItem(sender: UIBarButtonItem) {
30
32
 
31
33
 
32
34
 
33
- let actionSheet = UIAlertController(title: "", message: "選択してください", preferredStyle: .ActionSheet)
35
+ //UIImagePickerControllerを使うための定数
34
36
 
35
- let camera = UIAlertAction(title: "カメラ", style: .Default, handler: { _ in print("カメラ")})
37
+ let pickerController = UIImagePickerController()
36
38
 
37
- let library = UIAlertAction(title: "ライブラリ", style: .Default, handler: { _ in print("ライブラ")})
39
+ //UIImagePickerControllerのデゲートメソッドを使用する設定
38
40
 
39
- let cancel = UIAlertAction(title: "キャンセル", style: .Default, handler: { _ in print("キャンセル")})
41
+ pickerController.delegate = self
40
42
 
41
- actionSheet.addAction(camera)
43
+ //UIAcionSheetを使うための定数を作成
42
44
 
43
- actionSheet.addAction(library)
45
+ let sheet = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
44
-
45
- actionSheet.addAction(cancel)
46
46
 
47
47
 
48
48
 
49
- actionSheet.popoverPresentationController?.sourceView = view
49
+ //ボタンタイトルをNSLocalizedStringに変更
50
50
 
51
- actionSheet.popoverPresentationController?.sourceRect = sender.frame
51
+ let cancelString = NSLocalizedString("Cancel", comment: "キャンセル")
52
52
 
53
+ let cameraString = NSLocalizedString("Camera", comment: "カメラ")
54
+
55
+ let libraryString = NSLocalizedString("Library", comment: "ライブラリ")
56
+
57
+ //3つのアクションボタンの定数を作成
58
+
59
+ let cancelAction = UIAlertAction(title: cancelString, style: .Default, handler: {(action) -> Void in})
60
+
61
+ let cameraAction = UIAlertAction(title: cameraString, style: .Default, handler: {(action) -> Void in
62
+
63
+ pickerController.sourceType = .Camera
64
+
65
+ self.presentViewController(pickerController, animated: true, completion: nil)})
66
+
67
+ let LibraryAction = UIAlertAction(title: libraryString, style: .Default, handler: {(action) -> Void in
68
+
69
+ pickerController.sourceType = .PhotoLibrary
70
+
71
+ self.presentViewController(pickerController, animated: true, completion: nil)})
72
+
73
+
74
+
75
+
76
+
77
+ //アクションシートにアクションボタンを追加
78
+
79
+
80
+
81
+ sheet.addAction(cameraAction)
82
+
83
+ sheet.addAction(LibraryAction)
84
+
85
+ sheet.addAction(cancelAction)
86
+
87
+
88
+
89
+ //アクションシートを表示
90
+
91
+ sheet.popoverPresentationController?.sourceView = view
92
+
93
+ sheet.popoverPresentationController?.barButtonItem = sender
94
+
95
+
96
+
53
- presentViewController(actionSheet, animated: true, completion: nil)
97
+ self.presentViewController(sheet, animated: true, completion: nil)
54
98
 
55
99
  }
56
100
 
@@ -65,3 +109,35 @@
65
109
 
66
110
 
67
111
  ![i](51597db3c98fc7151c723214beae54d5.png)
112
+
113
+
114
+
115
+ `UIBarbuttonItem`に`arguments`を設定することで引数に`UIBarbuttonItem`自身のオブジェクトが渡されてきます。(`UIButton`の場合も同じ)
116
+
117
+ そのオブジェクトに対してアクションシートの位置を設定したりしたり、Buttonのtagで処理を分けたりします。
118
+
119
+ `@IBAction fund メソッド名(sender: UIBarButtonItem)`
120
+
121
+
122
+
123
+ ```swift
124
+
125
+ @IBAction fund メソッド名(sender: UIButton) {
126
+
127
+ if sender.tag == 1 {
128
+
129
+ // Buttonのtagが1の場合の処理
130
+
131
+ } else if sender.tag == 2 {
132
+
133
+ // Buttonのtagが2の場合の処理
134
+
135
+ }
136
+
137
+ }
138
+
139
+ ```
140
+
141
+
142
+
143
+

4

修正

2016/07/17 05:15

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -60,7 +60,7 @@
60
60
 
61
61
 
62
62
 
63
- ボタンの`IBAction`を結ぶときは以下のように`Argumets`を`Sender`に設定してみてください。
63
+ ボタンの`IBAction`を結ぶときは以下のように`Arguments`を`Sender`に設定してみてください。
64
64
 
65
65
 
66
66
 

3

修正

2016/07/16 13:41

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -60,6 +60,8 @@
60
60
 
61
61
 
62
62
 
63
- ボタンの`IBAction`を結ぶとき以下のように`Argumets`を`Sender`設定してみてください。
63
+ ボタンの`IBAction`を結ぶとき以下のように`Argumets`を`Sender`設定してみてください。
64
+
65
+
64
66
 
65
67
  ![i](51597db3c98fc7151c723214beae54d5.png)

2

修正

2016/07/16 13:40

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -57,3 +57,9 @@
57
57
  }
58
58
 
59
59
  ```
60
+
61
+
62
+
63
+ ボタンの`IBAction`を結ぶときに以下のように`Argumets`を`Sender`設定してみてください。
64
+
65
+ ![i](51597db3c98fc7151c723214beae54d5.png)

1

修正

2016/07/16 13:38

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- 上記のサイトを参考にして``ActionSheet`を表示するところまで確認しました。
5
+ 上記のサイトを参考にして`iPad Pro(シュミレーター)`で`ActionSheet`を表示するところまで確認しました。
6
6
 
7
7
 
8
8