質問編集履歴
1
コードの追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,4 +9,111 @@
|
|
9
9
|
```
|
10
10
|
**やりたい事**
|
11
11
|
label.text の部分のエラーを取りたい。
|
12
|
-
UILabelだとどうしてもエラーが出てしまうのでどうすればいいのかを知りたい
|
12
|
+
UILabelだとどうしてもエラーが出てしまうのでどうすればいいのかを知りたい
|
13
|
+
|
14
|
+
```ここに言語を入力
|
15
|
+
import UIKit
|
16
|
+
|
17
|
+
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
|
18
|
+
|
19
|
+
@IBOutlet weak var mytableView: UITableView!
|
20
|
+
var add = UILabel()
|
21
|
+
let item = [""]
|
22
|
+
//var longPressRecognizer: UILongPressGestureRecognizer!
|
23
|
+
//+ボタン
|
24
|
+
@IBAction func addlabel(_ sender: Any) {
|
25
|
+
|
26
|
+
alert()
|
27
|
+
//label()
|
28
|
+
}
|
29
|
+
|
30
|
+
//ラベルについて
|
31
|
+
func label(){
|
32
|
+
add = UILabel(frame: CGRect(x: 130, y:250, width: 100, height:20))
|
33
|
+
//ラベルの大きさ、座標指定
|
34
|
+
//add.text = "labelです"
|
35
|
+
//文字を変更
|
36
|
+
|
37
|
+
add.backgroundColor = UIColor.lightGray
|
38
|
+
|
39
|
+
add.font = UIFont.systemFont(ofSize: 30)
|
40
|
+
//文字の大きさ
|
41
|
+
|
42
|
+
add.textColor = UIColor.black
|
43
|
+
//文字カラー
|
44
|
+
|
45
|
+
add.sizeToFit()
|
46
|
+
//文字数にあわせてlabelの大きさを変更(サイズが文字にフィットする)
|
47
|
+
|
48
|
+
self.view.addSubview(add)
|
49
|
+
//実際にviewに見える形でlabelが出現する
|
50
|
+
}
|
51
|
+
|
52
|
+
func alert(){
|
53
|
+
// テキストフィールド付きアラート表示
|
54
|
+
|
55
|
+
let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
|
56
|
+
|
57
|
+
// OKボタンの設定
|
58
|
+
let okAction = UIAlertAction(title: "OK", style: .default, handler: {
|
59
|
+
(action:UIAlertAction!) -> Void in
|
60
|
+
|
61
|
+
// OKを押した時入力されていたテキストを表示
|
62
|
+
if let textFields = alert.textFields {
|
63
|
+
|
64
|
+
// アラートに含まれるすべてのテキストフィールドを調べる
|
65
|
+
for textField in textFields {
|
66
|
+
self.label()
|
67
|
+
self.add.text = textField.text!
|
68
|
+
self.add.sizeToFit()
|
69
|
+
print(textField.text!)
|
70
|
+
}
|
71
|
+
}
|
72
|
+
})
|
73
|
+
alert.addAction(okAction)
|
74
|
+
|
75
|
+
// キャンセルボタンの設定
|
76
|
+
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
|
77
|
+
alert.addAction(cancelAction)
|
78
|
+
|
79
|
+
// テキストフィールドを追加
|
80
|
+
alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
|
81
|
+
textField.placeholder = "テキスト"
|
82
|
+
})
|
83
|
+
|
84
|
+
alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
|
85
|
+
|
86
|
+
// アラートを画面に表示
|
87
|
+
self.present(alert, animated: true, completion: nil)
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
92
|
+
return add
|
93
|
+
}
|
94
|
+
|
95
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
96
|
+
return item.count
|
97
|
+
}
|
98
|
+
|
99
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
100
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
|
101
|
+
let item = add
|
102
|
+
cell.textLabel?.text = item[indexPath.row]
|
103
|
+
return cell
|
104
|
+
}
|
105
|
+
|
106
|
+
|
107
|
+
override func viewDidLoad() {
|
108
|
+
super.viewDidLoad()
|
109
|
+
|
110
|
+
}
|
111
|
+
|
112
|
+
override func didReceiveMemoryWarning() {
|
113
|
+
super.didReceiveMemoryWarning()
|
114
|
+
// Dispose of any resources that can be recreated.
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
}
|
119
|
+
```
|