質問編集履歴

1

説明補足

2015/06/13 08:26

投稿

tamago0224
tamago0224

スコア71

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,219 @@
1
1
  swiftでアプリ開発の際、ViewController.swift内のviewDidLoad()の中でmain.storyboardのボタンなどを追加しました。
2
2
 
3
3
  そのとき、main.storyboardにボタンなどのUIをつけてViewController.swiftの関数と紐づけるような操作は可能でしょうか?
4
+
5
+ 下のコードは以下のurlを参考にさせていただきました。
6
+
7
+ http://ja.stackoverflow.com/questions/10931/swift%E3%81%A7%E9%9B%BB%E5%8D%93%E3%82%A2%E3%83%97%E3%83%AA%E9%96%8B%E7%99%BA
8
+
9
+
10
+
11
+ ```lang-swift
12
+
13
+
14
+
15
+ import UIKit
16
+
17
+ import Foundation
18
+
19
+
20
+
21
+ class ViewController: UIViewController {
22
+
23
+
24
+
25
+ //計算結果を表示するラベルを宣言
26
+
27
+ var resultLabel = UILabel()
28
+
29
+ let xButtonCount = 4 //一行に配置するボタンの数
30
+
31
+ let yButtonCount = 4
32
+
33
+ //画面の横幅サイズを格納するメンバ変数
34
+
35
+ let screenWidth:Double = Double(UIScreen.mainScreen().bounds.size.width)
36
+
37
+ //画面の縦
38
+
39
+ let screenHeight:Double = Double(UIScreen.mainScreen().bounds.size.height)
40
+
41
+ //ボタン間の余白
42
+
43
+ let buttonMargin = 10.0
44
+
45
+ //計算結果表示
46
+
47
+ var resultArea = 0.0
48
+
49
+
50
+
51
+ //二つの項のうち左の項が入力済みかどうかを見る
52
+
53
+ var isLeftNotEmpty: Bool = false
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+ override func viewDidLoad() {
64
+
65
+ super.viewDidLoad()
66
+
67
+ //画面全体の縦幅に応じて計算結果表示エリアの縦幅を決定
68
+
69
+ switch screenHeight{
70
+
71
+ case 480:
72
+
73
+ resultArea = 200.0
74
+
75
+ case 568:
76
+
77
+ resultArea = 250.0
78
+
79
+ case 667:
80
+
81
+ resultArea = 300.0
82
+
83
+ case 736:
84
+
85
+ resultArea = 350.0
86
+
87
+ default:
88
+
89
+ resultArea = 0.0
90
+
91
+ }
92
+
93
+ //計算結果のラベル
94
+
95
+ resultLabel.frame = CGRect(x:10, y:30, width:screenWidth-20, height:resultArea-30)
96
+
97
+
98
+
99
+ let buttonLabels = [
100
+
101
+ "7","8","9","×",
102
+
103
+ "4","5","6","-",
104
+
105
+ "1","2","3","+",
106
+
107
+ "0","C","÷","="
108
+
109
+ ]
110
+
111
+
112
+
113
+ for var y=0; y<yButtonCount; y++ {
114
+
115
+ for var x=0; x<xButtonCount; x++ {
116
+
117
+ //計算機のボタン作成
118
+
119
+ var button = UIButton()
120
+
121
+ //ボタンの横幅
122
+
123
+ var buttonWidth = (screenWidth - (buttonMargin * (Double(xButtonCount)+1)))/Double(xButtonCount)
124
+
125
+ //ボタンの縦幅
126
+
127
+ var buttonHeight = (screenHeight - resultArea - ((buttonMargin*Double(yButtonCount)+1)))/Double(yButtonCount)
128
+
129
+ //ボタンのx座標
130
+
131
+ var buttonPositionX = (screenWidth - buttonMargin) / Double(xButtonCount) * Double(x) + buttonMargin
132
+
133
+ //ボタンのy座標
134
+
135
+ var buttonPositionY = (screenHeight - resultArea - buttonMargin) / Double(yButtonCount) * Double(y) + buttonMargin + resultArea
136
+
137
+ //ボタンの配置、サイズ
138
+
139
+ button.frame = CGRect(x:buttonPositionX, y:buttonPositionY, width:buttonWidth, height:buttonHeight)
140
+
141
+ //背景
142
+
143
+ button.backgroundColor = UIColor.greenColor()
144
+
145
+ //ボタンのラベルタイトル
146
+
147
+ var buttonNumber = y * xButtonCount + x
148
+
149
+ //ボタンのラベルタイトルを取り出すインデックス番号
150
+
151
+ button.setTitle(buttonLabels[buttonNumber],forState: UIControlState.Normal)
152
+
153
+ //ボタンタップ時のアクション
154
+
155
+ button.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
156
+
157
+ //ボタン配置
158
+
159
+ self.view.addSubview(button)
160
+
161
+ }
162
+
163
+ }
164
+
165
+
166
+
167
+ //計算結果ラベル設定する
168
+
169
+ resultLabel.backgroundColor = UIColor.grayColor()
170
+
171
+ resultLabel.font = UIFont(name:"Arial", size: 50)
172
+
173
+ resultLabel.textAlignment = NSTextAlignment.Right
174
+
175
+ resultLabel.numberOfLines = 4
176
+
177
+ resultLabel.text = "0"
178
+
179
+
180
+
181
+ //計算結果ラベルをviewcontrollerクラスのviewに設置
182
+
183
+ self.view.addSubview(resultLabel)
184
+
185
+ }
186
+
187
+
188
+
189
+ override func didReceiveMemoryWarning() {
190
+
191
+ super.didReceiveMemoryWarning()
192
+
193
+ // Dispose of any resources that can be recreated.
194
+
195
+ }
196
+
197
+
198
+
199
+ //ボタンタップメソッド
200
+
201
+ func buttonTapped(sender:UIButton){
202
+
203
+ var tappedButtonTitle:String = sender.titleLabel!.text!
204
+
205
+ println("\(tappedButtonTitle)ボタンがタップされました")
206
+
207
+
208
+
209
+ }
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+ }
218
+
219
+ ```