質問編集履歴
2
シンタックスハイライトで再表記
test
CHANGED
File without changes
|
test
CHANGED
@@ -28,9 +28,91 @@
|
|
28
28
|
|
29
29
|
|
30
30
|
|
31
|
+
```
|
32
|
+
|
33
|
+
//
|
34
|
+
|
35
|
+
// ViewController.swift
|
36
|
+
|
37
|
+
// Calculator
|
38
|
+
|
39
|
+
//
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
import UIKit
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
class ViewController: UIViewController {
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
//計算結果を表示するラベルを宣言
|
52
|
+
|
53
|
+
var resultLabel = UILabel()
|
54
|
+
|
55
|
+
let xButtonCount = 4 //一行に配置するボタンの数
|
56
|
+
|
57
|
+
let yButtonCount = 4
|
58
|
+
|
59
|
+
//画面の横幅サイズを格納するメンバ変数
|
60
|
+
|
61
|
+
let screenWidth:Double = Double(UIScreen.mainScreen().bounds.size.width)
|
62
|
+
|
63
|
+
//画面の縦
|
64
|
+
|
65
|
+
let screenHeight:Double = Double(UIScreen.mainScreen().bounds.size.height)
|
66
|
+
|
67
|
+
//ボタン間の余白
|
68
|
+
|
69
|
+
let buttonMargin = 10.0
|
70
|
+
|
71
|
+
//計算結果表示
|
72
|
+
|
73
|
+
var resultArea = 0.0
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
override func viewDidLoad() {
|
80
|
+
|
81
|
+
super.viewDidLoad()
|
82
|
+
|
83
|
+
//画面全体の縦幅に応じて計算結果表示エリアの縦幅を決定
|
84
|
+
|
85
|
+
switch screenHeight{
|
86
|
+
|
87
|
+
case 480:
|
88
|
+
|
89
|
+
resultArea = 200.0
|
90
|
+
|
91
|
+
case 568:
|
92
|
+
|
93
|
+
resultArea = 250.0
|
94
|
+
|
95
|
+
case 667:
|
96
|
+
|
97
|
+
resultArea = 300.0
|
98
|
+
|
99
|
+
case 736:
|
100
|
+
|
101
|
+
resultArea = 350.0
|
102
|
+
|
31
|
-
|
103
|
+
default:
|
104
|
+
|
32
|
-
|
105
|
+
resultArea = 0.0
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
//計算結果のラベル
|
110
|
+
|
111
|
+
resultLabel.frame = CGRect(x:10, y:30, width:screenWidth-20, height:resultArea-30)
|
112
|
+
|
113
|
+
|
114
|
+
|
33
|
-
let buttonLabels = [
|
115
|
+
let buttonLabels = [
|
34
116
|
|
35
117
|
"7","8","9","×",
|
36
118
|
|
@@ -44,70 +126,114 @@
|
|
44
126
|
|
45
127
|
|
46
128
|
|
47
|
-
for var y=0; y<yButtonCount; y++ {
|
48
|
-
|
49
|
-
for var x=0; x<xButtonCount; x++ {
|
50
|
-
|
51
|
-
//計算機のボタン作成
|
52
|
-
|
53
|
-
var button = UIButton()
|
54
|
-
|
55
|
-
//ボタンの横幅
|
56
|
-
|
57
|
-
var buttonWidth = (screenWidth - (buttonMargin * (Double(xButtonCount)+1)))/Double(xButtonCount)
|
58
|
-
|
59
|
-
//ボタンの縦幅
|
60
|
-
|
61
|
-
var buttonHeight = (screenHeight - resultArea - ((buttonMargin*Double(yButtonCount)+1)))/Double(yButtonCount)
|
62
|
-
|
63
|
-
//ボタンのx座標
|
64
|
-
|
65
|
-
var buttonPositionX = (screenWidth - buttonMargin) / Double(xButtonCount) * Double(x) + buttonMargin
|
66
|
-
|
67
|
-
//ボタンのy座標
|
68
|
-
|
69
|
-
var buttonPositionY = (screenHeight - resultArea - buttonMargin) / Double(yButtonCount) * Double(y) + buttonMargin + resultArea
|
70
|
-
|
71
|
-
//ボタンの配置、サイズ
|
72
|
-
|
73
|
-
button.frame = CGRect(x:buttonPositionX, y:buttonPositionY, width:buttonWidth, height:buttonHeight)
|
74
|
-
|
75
|
-
//背景
|
76
|
-
|
77
|
-
button.backgroundColor = UIColor.greenColor()
|
78
|
-
|
79
|
-
//ボタンのラベルタイトル
|
80
|
-
|
81
|
-
var buttonNumber = y * xButtonCount + x
|
82
|
-
|
83
|
-
//ボタンのラベルタイトルを取り出すインデックス番号
|
84
|
-
|
85
|
-
button.setTitle(buttonLabels[buttonNumber],forState: UIControlState.Normal)
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
//ボタン配置
|
92
|
-
|
93
|
-
self.view.addSubview(button)
|
94
|
-
|
95
|
-
|
129
|
+
for var y=0; y<yButtonCount; y++ {
|
130
|
+
|
131
|
+
for var x=0; x<xButtonCount; x++ {
|
132
|
+
|
133
|
+
//計算機のボタン作成
|
134
|
+
|
135
|
+
var button = UIButton()
|
136
|
+
|
137
|
+
//ボタンの横幅
|
138
|
+
|
139
|
+
var buttonWidth = (screenWidth - (buttonMargin * (Double(xButtonCount)+1)))/Double(xButtonCount)
|
140
|
+
|
141
|
+
//ボタンの縦幅
|
142
|
+
|
143
|
+
var buttonHeight = (screenHeight - resultArea - ((buttonMargin*Double(yButtonCount)+1)))/Double(yButtonCount)
|
144
|
+
|
145
|
+
//ボタンのx座標
|
146
|
+
|
147
|
+
var buttonPositionX = (screenWidth - buttonMargin) / Double(xButtonCount) * Double(x) + buttonMargin
|
148
|
+
|
149
|
+
//ボタンのy座標
|
150
|
+
|
151
|
+
var buttonPositionY = (screenHeight - resultArea - buttonMargin) / Double(yButtonCount) * Double(y) + buttonMargin + resultArea
|
152
|
+
|
153
|
+
//ボタンの配置、サイズ
|
154
|
+
|
155
|
+
button.frame = CGRect(x:buttonPositionX, y:buttonPositionY, width:buttonWidth, height:buttonHeight)
|
156
|
+
|
157
|
+
//背景
|
158
|
+
|
159
|
+
button.backgroundColor = UIColor.greenColor()
|
160
|
+
|
161
|
+
//ボタンのラベルタイトル
|
162
|
+
|
163
|
+
var buttonNumber = y * xButtonCount + x
|
164
|
+
|
165
|
+
//ボタンのラベルタイトルを取り出すインデックス番号
|
166
|
+
|
167
|
+
button.setTitle(buttonLabels[buttonNumber],forState: UIControlState.Normal)
|
168
|
+
|
169
|
+
//ボタンタップ時のアクション
|
170
|
+
|
171
|
+
button.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
|
172
|
+
|
173
|
+
//ボタン配置
|
174
|
+
|
175
|
+
self.view.addSubview(button)
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
//計算結果ラベル設定する
|
186
|
+
|
187
|
+
resultLabel.backgroundColor = UIColor.grayColor()
|
188
|
+
|
189
|
+
resultLabel.font = UIFont(name:"Arial", size: 50)
|
190
|
+
|
191
|
+
resultLabel.textAlignment = NSTextAlignment.Right
|
192
|
+
|
193
|
+
resultLabel.numberOfLines = 4
|
194
|
+
|
195
|
+
resultLabel.text = "0"
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
//計算結果ラベルをviewcontrollerクラスのviewに設置
|
200
|
+
|
201
|
+
self.view.addSubview(resultLabel)
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
//ボタンタップメソッド
|
206
|
+
|
207
|
+
func buttonTapped(sender:UIButton){
|
208
|
+
|
209
|
+
var tappedButtonTitle:String = sender.titleLabel!.text!
|
210
|
+
|
211
|
+
println("\(tappedButtonTitle)ボタンがタップされました")
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
override func didReceiveMemoryWarning() {
|
226
|
+
|
227
|
+
super.didReceiveMemoryWarning()
|
228
|
+
|
229
|
+
// Dispose of any resources that can be recreated.
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
|
96
236
|
|
97
237
|
}
|
98
238
|
|
99
|
-
|
100
|
-
|
101
|
-
|
239
|
+
```
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
//ボタンタップメソッド
|
106
|
-
|
107
|
-
func buttonTapped(sender:UIButton){
|
108
|
-
|
109
|
-
var tappedButtonTitle:String = sender.currentTitle!
|
110
|
-
|
111
|
-
println("\(tappedButtonTitle)ボタンがタップされました")
|
112
|
-
|
113
|
-
}
|
1
誤字修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -15,6 +15,8 @@
|
|
15
15
|
**[Calculator.ViewController buttonTapped:]: unrecognized selector sent to instance 0x7988fea0**
|
16
16
|
|
17
17
|
buttonTappedメソッドが認識できないと言われてしまっています。
|
18
|
+
|
19
|
+
誤字脱字は確認したので、そちらは問題ないように思われます。認識できないのか、わかりません、、、。
|
18
20
|
|
19
21
|
|
20
22
|
|