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

質問編集履歴

1

説明補足

2015/06/13 08:26

投稿

tamago0224
tamago0224

スコア71

title CHANGED
File without changes
body CHANGED
@@ -1,2 +1,110 @@
1
1
  swiftでアプリ開発の際、ViewController.swift内のviewDidLoad()の中でmain.storyboardのボタンなどを追加しました。
2
- そのとき、main.storyboardにボタンなどのUIをつけてViewController.swiftの関数と紐づけるような操作は可能でしょうか?
2
+ そのとき、main.storyboardにボタンなどのUIをつけてViewController.swiftの関数と紐づけるような操作は可能でしょうか?
3
+ 下のコードは以下のurlを参考にさせていただきました。
4
+ 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
5
+
6
+ ```lang-swift
7
+
8
+ import UIKit
9
+ import Foundation
10
+
11
+ class ViewController: UIViewController {
12
+
13
+ //計算結果を表示するラベルを宣言
14
+ var resultLabel = UILabel()
15
+ let xButtonCount = 4 //一行に配置するボタンの数
16
+ let yButtonCount = 4
17
+ //画面の横幅サイズを格納するメンバ変数
18
+ let screenWidth:Double = Double(UIScreen.mainScreen().bounds.size.width)
19
+ //画面の縦
20
+ let screenHeight:Double = Double(UIScreen.mainScreen().bounds.size.height)
21
+ //ボタン間の余白
22
+ let buttonMargin = 10.0
23
+ //計算結果表示
24
+ var resultArea = 0.0
25
+
26
+ //二つの項のうち左の項が入力済みかどうかを見る
27
+ var isLeftNotEmpty: Bool = false
28
+
29
+
30
+
31
+
32
+ override func viewDidLoad() {
33
+ super.viewDidLoad()
34
+ //画面全体の縦幅に応じて計算結果表示エリアの縦幅を決定
35
+ switch screenHeight{
36
+ case 480:
37
+ resultArea = 200.0
38
+ case 568:
39
+ resultArea = 250.0
40
+ case 667:
41
+ resultArea = 300.0
42
+ case 736:
43
+ resultArea = 350.0
44
+ default:
45
+ resultArea = 0.0
46
+ }
47
+ //計算結果のラベル
48
+ resultLabel.frame = CGRect(x:10, y:30, width:screenWidth-20, height:resultArea-30)
49
+
50
+ let buttonLabels = [
51
+ "7","8","9","×",
52
+ "4","5","6","-",
53
+ "1","2","3","+",
54
+ "0","C","÷","="
55
+ ]
56
+
57
+ for var y=0; y<yButtonCount; y++ {
58
+ for var x=0; x<xButtonCount; x++ {
59
+ //計算機のボタン作成
60
+ var button = UIButton()
61
+ //ボタンの横幅
62
+ var buttonWidth = (screenWidth - (buttonMargin * (Double(xButtonCount)+1)))/Double(xButtonCount)
63
+ //ボタンの縦幅
64
+ var buttonHeight = (screenHeight - resultArea - ((buttonMargin*Double(yButtonCount)+1)))/Double(yButtonCount)
65
+ //ボタンのx座標
66
+ var buttonPositionX = (screenWidth - buttonMargin) / Double(xButtonCount) * Double(x) + buttonMargin
67
+ //ボタンのy座標
68
+ var buttonPositionY = (screenHeight - resultArea - buttonMargin) / Double(yButtonCount) * Double(y) + buttonMargin + resultArea
69
+ //ボタンの配置、サイズ
70
+ button.frame = CGRect(x:buttonPositionX, y:buttonPositionY, width:buttonWidth, height:buttonHeight)
71
+ //背景
72
+ button.backgroundColor = UIColor.greenColor()
73
+ //ボタンのラベルタイトル
74
+ var buttonNumber = y * xButtonCount + x
75
+ //ボタンのラベルタイトルを取り出すインデックス番号
76
+ button.setTitle(buttonLabels[buttonNumber],forState: UIControlState.Normal)
77
+ //ボタンタップ時のアクション
78
+ button.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
79
+ //ボタン配置
80
+ self.view.addSubview(button)
81
+ }
82
+ }
83
+
84
+ //計算結果ラベル設定する
85
+ resultLabel.backgroundColor = UIColor.grayColor()
86
+ resultLabel.font = UIFont(name:"Arial", size: 50)
87
+ resultLabel.textAlignment = NSTextAlignment.Right
88
+ resultLabel.numberOfLines = 4
89
+ resultLabel.text = "0"
90
+
91
+ //計算結果ラベルをviewcontrollerクラスのviewに設置
92
+ self.view.addSubview(resultLabel)
93
+ }
94
+
95
+ override func didReceiveMemoryWarning() {
96
+ super.didReceiveMemoryWarning()
97
+ // Dispose of any resources that can be recreated.
98
+ }
99
+
100
+ //ボタンタップメソッド
101
+ func buttonTapped(sender:UIButton){
102
+ var tappedButtonTitle:String = sender.titleLabel!.text!
103
+ println("\(tappedButtonTitle)ボタンがタップされました")
104
+
105
+ }
106
+
107
+
108
+
109
+ }
110
+ ```