以下の2点を修正することで"0"が表示されます。
swift
1
2func Calculation(){
3 // ボタン生成等・・・
4}
5
6// 変更①
7// ViewControllerのメソッドとして呼び出せる様にCalculation()メソッドのブロックから外に出します。
8func num(sender:UIButton){
9 switch sender.tag {
10 case 0 :
11 labelresult.text = "0"
12 default:break
13 }
14}
15
16// 上記を修正するとlabelresultが定義されていないとエラーになりますので、以下の様にlabelresultをインスタンス変数で定義します。
17// ※ Calculation()の中にある let labelresult = UILabel()は消してください。
18// 変更②
19import UIKit
20
21class ViewController: UIViewController {
22 let labelresult = UILabel()
23 // ・・・
24}
よくよく見ると今回の回答は
前回の質問(Thread 1 : breakpoint 1.1)のTakeOneさんの回答1と2ですね。
回答追記
swift
1import UIKit
2
3class ViewController: UIViewController {
4
5 let labelresult = UILabel() //計算結果を表示させる
6
7 override func viewDidLoad() {
8 super.viewDidLoad()
9 // Do any additional setup after loading the view, typically from a nib.
10 self.Calculation()
11 }
12
13 func Calculation() {
14
15 let backView = UIView() //背景
16 backView.frame = CGRect(x:0.8, y:20.0, width:373.0, height:646.0)
17 backView.backgroundColor = UIColor.whiteColor()
18 self.view.addSubview(backView)
19
20 let label0 = UIButton() //1
21 label0.frame = CGRectMake(230, 10, 40, 40)
22 label0.backgroundColor = UIColor.blackColor()
23 label0.setTitle("0", forState: .Normal)
24 label0.setTitleColor(UIColor.cyanColor(), forState: UIControlState .Normal)
25 label0.titleLabel!.font = UIFont.systemFontOfSize(29)
26 label0.addTarget(self, action: #selector(ViewController.num(_:)), forControlEvents: UIControlEvents .TouchDown)
27 label0.tag = 0
28 backView.addSubview(label0)
29
30 let label1 = UIButton() //1
31 label1.frame = CGRectMake(1, 272, 124, 124)
32 label1.backgroundColor = UIColor.redColor()
33 label1.setTitle("1", forState: .Normal)
34 label1.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
35 label1.titleLabel!.font = UIFont.systemFontOfSize(29)
36 label1.tag = 1
37 backView.addSubview(label1)
38
39 let label2 = UIButton() //2
40 label2.frame = CGRectMake(126, 272, 124, 124)
41 label2.backgroundColor = UIColor.redColor()
42 label2.setTitle("2", forState: .Normal)
43 label2.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
44 label2.titleLabel!.font = UIFont.systemFontOfSize(29)
45 label2.tag = 2
46 backView.addSubview(label2)
47
48 let label3 = UIButton() //3
49 label3.frame = CGRectMake(251,272, 124, 124)
50 label3.backgroundColor = UIColor.redColor()
51 label3.setTitle("3", forState: .Normal)
52 label3.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
53 label3.titleLabel!.font = UIFont.systemFontOfSize(29)
54 label3.tag = 3
55 backView.addSubview(label3)
56
57 let label4 = UIButton() //4
58 label4.frame = CGRectMake( 1, 397, 124, 124)
59 label4.backgroundColor = UIColor.blackColor()
60 label4.setTitle("4", forState: .Normal)
61 label4.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
62 label4.titleLabel!.font = UIFont.systemFontOfSize(29)
63 label4.tag = 4
64 backView.addSubview(label4)
65
66 let label5 = UIButton() //5
67 label5.frame = CGRectMake(126,397, 124, 124)
68 label5.backgroundColor = UIColor.blackColor()
69 label5.setTitle("5", forState: .Normal)
70 label5.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
71 label5.titleLabel!.font = UIFont.systemFontOfSize(29)
72 label5.tag = 5
73 backView.addSubview(label5)
74
75 let label6 = UIButton() //6
76 label6.frame = CGRectMake(251, 397, 124, 124)
77 label6.backgroundColor = UIColor.blackColor()
78 label6.setTitle("6", forState: .Normal)
79 label6.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
80 label6.titleLabel!.font = UIFont.systemFontOfSize(29)
81 label6.tag = 6
82 backView.addSubview(label6)
83
84 let label7 = UIButton() //7
85 label7.frame = CGRectMake(1, 522, 124, 124)
86 label7.backgroundColor = UIColor.blueColor()
87 label7.setTitle("7", forState: .Normal)
88 label7.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
89 label7.titleLabel!.font = UIFont.systemFontOfSize(29)
90 label7.tag = 7
91 backView.addSubview(label7)
92
93 let label8 = UIButton() //8
94 label8.frame = CGRectMake(126, 522, 124, 124)
95 label8.backgroundColor = UIColor.blueColor()
96 label8.setTitle("8", forState: .Normal)
97 label8.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
98 label8.titleLabel!.font = UIFont.systemFontOfSize(29)
99 label6.tag = 8
100 backView.addSubview(label8)
101
102 let label9 = UIButton() //9
103 label9.frame = CGRectMake(251, 522, 124, 124)
104 label9.backgroundColor = UIColor.blueColor()
105 label9.setTitle("9", forState: .Normal)
106 label9.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
107 label9.titleLabel!.font = UIFont.systemFontOfSize(29)
108 label6.tag = 9
109 backView.addSubview(label9)
110
111 //足し算
112 let labelplus = UIButton()
113 labelplus.frame = CGRect(x: 10, y: 10, width: 40, height: 40)
114 labelplus.backgroundColor = UIColor.blackColor()
115 labelplus.setTitle("+", forState: .Normal)
116 labelplus.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
117 labelplus.titleLabel!.font = UIFont.systemFontOfSize(29)
118 labelplus.tag = 01
119 backView.addSubview(labelplus)
120
121 let labelminus = UIButton() //引き算
122 labelminus.frame = CGRect(x: 60, y: 10, width: 40, height: 40)
123 labelminus.backgroundColor = UIColor.blackColor()
124 labelminus.setTitle("-", forState: .Normal)
125 labelminus.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
126 labelminus.titleLabel!.font = UIFont.systemFontOfSize(29)
127 labelminus.tag = 02
128 backView.addSubview(labelminus)
129
130 let labelproduct = UIButton() //掛け算
131 labelproduct.frame = CGRect(x: 110, y: 10, width: 40, height: 40)
132 labelproduct.backgroundColor = UIColor.blackColor()
133 labelproduct.setTitle("x", forState: .Normal)
134 labelproduct.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
135 labelproduct.titleLabel!.font = UIFont.systemFontOfSize(29)
136 labelproduct.tag = 03
137 backView.addSubview(labelproduct)
138
139 let labelquontient = UIButton() //割り算
140 labelquontient.frame = CGRect(x: 180, y: 10, width: 40, height: 40)
141 labelquontient.backgroundColor = UIColor.blackColor()
142 labelquontient.setTitle("/", forState: .Normal)
143 labelquontient.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
144 labelquontient.titleLabel!.font = UIFont.systemFontOfSize(29)
145 labelquontient.tag = 04
146 backView.addSubview(labelquontient)
147
148 let labelclear = UIButton() //現在の計算結果を0にリセット
149 labelclear.frame = CGRect(x: 280, y: 10, width: 40, height: 40)
150 labelclear.backgroundColor = UIColor.blackColor()
151 labelclear.setTitle("C", forState: .Normal)
152 labelclear.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal)
153 labelclear.titleLabel!.font = UIFont.systemFontOfSize(29)
154 labelclear.tag = 05
155 backView.addSubview(labelclear)
156
157
158 labelresult.frame = CGRect(x: 1, y: 60, width: 370, height: 210)
159 labelresult.backgroundColor = UIColor.lightGrayColor()
160 backView.addSubview(labelresult)
161 }
162
163 func num(sender:UIButton){
164 switch sender.tag {
165 case 0:
166 labelresult.text = "0"
167 default: break
168 }
169 }
170
171 override func didReceiveMemoryWarning() {
172 super.didReceiveMemoryWarning()
173 // Dispose of any resources that can be recreated.
174 }
175
176}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/03 14:30
2016/06/03 14:46