teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

追記

2016/04/02 02:59

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -5,6 +5,8 @@
5
5
 
6
6
  class ViewController: UIViewController {
7
7
 
8
+ let buttoArray: [UIButton] = []
9
+
8
10
  override func viewDidLoad() {
9
11
  super.viewDidLoad()
10
12
 
@@ -24,7 +26,6 @@
24
26
  button2.backgroundColor = UIColor.redColor()
25
27
  button2.tag = 2
26
28
 
27
-
28
29
  // Button3を作成
29
30
  let button3 = UIButton(type: .Custom)
30
31
  button3.frame = CGRectMake(100, 300, 200, 50)
@@ -33,9 +34,8 @@
33
34
  button3.backgroundColor = UIColor.greenColor()
34
35
  button3.tag = 3
35
36
 
36
-
37
37
  // UIButtonの配列
38
- let buttoArray = [button1, button2, button3]
38
+ buttoArray = [button1, button2, button3]
39
39
 
40
40
 
41
41
  // 配列から取り出してViewに追加
@@ -63,18 +63,15 @@
63
63
 
64
64
  class ViewController: UIViewController {
65
65
 
66
+ let buttoArray: [UIButton] = []
67
+
66
68
  override func viewDidLoad() {
67
69
  super.viewDidLoad()
68
70
 
69
- // UIButtonの配列
70
- var buttoArray: [UIButton] = []
71
-
72
71
  // 作成するボタンの数
73
72
  let buttonCount = 5
74
73
 
75
-
76
74
  for index in 0 ..< buttonCount {
77
-
78
75
  // Buttonを作成
79
76
  let button = UIButton(type: .Custom)
80
77
  button.frame = CGRectMake(100, 100 * CGFloat(index) + 50, 200, 50)
@@ -85,7 +82,6 @@
85
82
  buttoArray.append(button)
86
83
  }
87
84
 
88
-
89
85
  // 配列から取り出してViewに追加
90
86
  for button in buttoArray {
91
87
  view.addSubview(button)

2

追記

2016/04/02 02:59

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -52,4 +52,51 @@
52
52
  ```
53
53
 
54
54
  実行結果
55
- ![image](612f0212b0c5ac3dfa18a5f352d1daab.png)
55
+ ![image](612f0212b0c5ac3dfa18a5f352d1daab.png)
56
+
57
+
58
+ 量産型
59
+ ---
60
+
61
+ ```swift
62
+ import UIKit
63
+
64
+ class ViewController: UIViewController {
65
+
66
+ override func viewDidLoad() {
67
+ super.viewDidLoad()
68
+
69
+ // UIButtonの配列
70
+ var buttoArray: [UIButton] = []
71
+
72
+ // 作成するボタンの数
73
+ let buttonCount = 5
74
+
75
+
76
+ for index in 0 ..< buttonCount {
77
+
78
+ // Buttonを作成
79
+ let button = UIButton(type: .Custom)
80
+ button.frame = CGRectMake(100, 100 * CGFloat(index) + 50, 200, 50)
81
+ button.setTitle("button", forState: .Normal)
82
+ button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
83
+ button.backgroundColor = UIColor.blueColor()
84
+ button.tag = index
85
+ buttoArray.append(button)
86
+ }
87
+
88
+
89
+ // 配列から取り出してViewに追加
90
+ for button in buttoArray {
91
+ view.addSubview(button)
92
+ }
93
+ }
94
+
95
+ override func didReceiveMemoryWarning() {
96
+ super.didReceiveMemoryWarning()
97
+
98
+ }
99
+ }
100
+ ```
101
+
102
+ ![aaa](1cd99ae1e11229d14e1bbc0741af1e71.png)

1

修正

2016/04/02 02:54

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -51,5 +51,5 @@
51
51
  }
52
52
  ```
53
53
 
54
-
54
+ 実行結果
55
55
  ![image](612f0212b0c5ac3dfa18a5f352d1daab.png)