私は電卓アプリを制作しています。まず、"1"などの自然数が書かれたボタンを押した時に表示画面にその数字を表示させる、という処理をコードで書こうと思いました。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.Calculation() } func Calculation() { let backView = UIView() //背景 backView.frame = CGRect(x:0.8, y:20.0, width:373.0, height:646.0) backView.backgroundColor = UIColor.whiteColor() self.view.addSubview(backView) let label0 = UIButton() //1 label0.frame = CGRectMake(230, 10, 40, 40) label0.backgroundColor = UIColor.blackColor() label0.setTitle("0", forState: .Normal) label0.setTitleColor(UIColor.cyanColor(), forState: UIControlState .Normal) label0.titleLabel!.font = UIFont.systemFontOfSize(29) label0.tag = 0 backView.addSubview(label0) let label1 = UIButton() //1 label1.frame = CGRectMake(1, 272, 124, 124) label1.backgroundColor = UIColor.redColor() label1.setTitle("1", forState: .Normal) label1.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) label1.titleLabel!.font = UIFont.systemFontOfSize(29) label1.tag = 1 backView.addSubview(label1) let label2 = UIButton() //2 label2.frame = CGRectMake(126, 272, 124, 124) label2.backgroundColor = UIColor.redColor() label2.setTitle("2", forState: .Normal) label2.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) label2.titleLabel!.font = UIFont.systemFontOfSize(29) label2.tag = 2 backView.addSubview(label2) let label3 = UIButton() //3 label3.frame = CGRectMake(251,272, 124, 124) label3.backgroundColor = UIColor.redColor() label3.setTitle("3", forState: .Normal) label3.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) label3.titleLabel!.font = UIFont.systemFontOfSize(29) label3.tag = 3 backView.addSubview(label3) let label4 = UIButton() //4 label4.frame = CGRectMake( 1, 397, 124, 124) label4.backgroundColor = UIColor.blackColor() label4.setTitle("4", forState: .Normal) label4.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) label4.titleLabel!.font = UIFont.systemFontOfSize(29) label4.tag = 4 backView.addSubview(label4) let label5 = UIButton() //5 label5.frame = CGRectMake(126,397, 124, 124) label5.backgroundColor = UIColor.blackColor() label5.setTitle("5", forState: .Normal) label5.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) label5.titleLabel!.font = UIFont.systemFontOfSize(29) label5.tag = 5 backView.addSubview(label5) let label6 = UIButton() //6 label6.frame = CGRectMake(251, 397, 124, 124) label6.backgroundColor = UIColor.blackColor() label6.setTitle("6", forState: .Normal) label6.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) label6.titleLabel!.font = UIFont.systemFontOfSize(29) label6.tag = 6 backView.addSubview(label6) let label7 = UIButton() //7 label7.frame = CGRectMake(1, 522, 124, 124) label7.backgroundColor = UIColor.blueColor() label7.setTitle("7", forState: .Normal) label7.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) label7.titleLabel!.font = UIFont.systemFontOfSize(29) label7.tag = 7 backView.addSubview(label7) let label8 = UIButton() //8 label8.frame = CGRectMake(126, 522, 124, 124) label8.backgroundColor = UIColor.blueColor() label8.setTitle("8", forState: .Normal) label8.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) label8.titleLabel!.font = UIFont.systemFontOfSize(29) label6.tag = 8 backView.addSubview(label8) let label9 = UIButton() //9 label9.frame = CGRectMake(251, 522, 124, 124) label9.backgroundColor = UIColor.blueColor() label9.setTitle("9", forState: .Normal) label9.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) label9.titleLabel!.font = UIFont.systemFontOfSize(29) label6.tag = 9 backView.addSubview(label9) //足し算 let labelplus = UIButton() labelplus.frame = CGRect(x: 10, y: 10, width: 40, height: 40) labelplus.backgroundColor = UIColor.blackColor() labelplus.setTitle("+", forState: .Normal) labelplus.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) labelplus.titleLabel!.font = UIFont.systemFontOfSize(29) labelplus.tag = 01 backView.addSubview(labelplus) let labelminus = UIButton() //引き算 labelminus.frame = CGRect(x: 60, y: 10, width: 40, height: 40) labelminus.backgroundColor = UIColor.blackColor() labelminus.setTitle("-", forState: .Normal) labelminus.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) labelminus.titleLabel!.font = UIFont.systemFontOfSize(29) labelminus.tag = 02 backView.addSubview(labelminus) let labelproduct = UIButton() //掛け算 labelproduct.frame = CGRect(x: 110, y: 10, width: 40, height: 40) labelproduct.backgroundColor = UIColor.blackColor() labelproduct.setTitle("x", forState: .Normal) labelproduct.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) labelproduct.titleLabel!.font = UIFont.systemFontOfSize(29) labelproduct.tag = 03 backView.addSubview(labelproduct) let labelquontient = UIButton() //割り算 labelquontient.frame = CGRect(x: 180, y: 10, width: 40, height: 40) labelquontient.backgroundColor = UIColor.blackColor() labelquontient.setTitle("/", forState: .Normal) labelquontient.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) labelquontient.titleLabel!.font = UIFont.systemFontOfSize(29) labelquontient.tag = 04 backView.addSubview(labelquontient) let labelclear = UIButton() //現在の計算結果を0にリセット labelclear.frame = CGRect(x: 280, y: 10, width: 40, height: 40) labelclear.backgroundColor = UIColor.blackColor() labelclear.setTitle("C", forState: .Normal) labelclear.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Normal) labelclear.titleLabel!.font = UIFont.systemFontOfSize(29) labelclear.tag = 05 backView.addSubview(labelclear) let labelresult = UILabel() //計算結果を表示させる labelresult.frame = CGRect(x: 1, y: 60, width: 370, height: 210) labelresult.backgroundColor = UIColor.lightGrayColor() backView.addSubview(labelresult) func num(sender:UIButton){ switch sender.tag { case 0: labelresult.text = "0" default: break } } } //ここです! override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
}
しかし、iPhone 6s でシミュレーターを起動してみると、エディタ画面に戻ってしまい、Thread 1 :breakpoint1.1というエラーが出てしまいます。control + command + y をしてみましたが、処理が実行されません。何が原因なのか全くわかりません。どうかご回答よろしくお願いしますm(_)m
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/03 08:25