質問編集履歴
1
前回、実現できたコードを入力しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
-
@IBOutlet weak var createUILabel: UIButton! // UILbelを
|
15
|
+
@IBOutlet weak var createUILabel: UIButton! // UILabelを出力するボタン
|
16
16
|
|
17
17
|
@IBOutlet weak var loadButton: UIButton! //ロードボタン
|
18
18
|
|
@@ -48,7 +48,7 @@
|
|
48
48
|
|
49
49
|
|
50
50
|
|
51
|
-
//UILabelを
|
51
|
+
//UILabelを出力する処理
|
52
52
|
|
53
53
|
@IBAction func actionCreateUIButton(_ sender: UIButton) {
|
54
54
|
|
@@ -109,3 +109,139 @@
|
|
109
109
|
|
110
110
|
|
111
111
|
```
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
UIlabelを保存というのは良くない表現でした。UIlabelのプロパティを保存するという意味でした。失礼しました。
|
118
|
+
|
119
|
+
以下、UILabelをuserDefaultsに保存することは実現できたコードです。
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
```swift
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
import UIKit
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
class ViewController: UIViewController {
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
@IBOutlet weak var createButton: UIButton! //UILabelを出力するボタン
|
136
|
+
|
137
|
+
@IBOutlet weak var loadButton: UIButton! //ロードボタン
|
138
|
+
|
139
|
+
@IBOutlet weak var saveButton: UIButton! //セーブボタン
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
let save = UserDefaults.standard
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
let testLabel = UILabel()
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
override func viewDidLoad() {
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
super.viewDidLoad()
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
createButton.backgroundColor = UIColor.darkGray
|
160
|
+
|
161
|
+
loadButton.backgroundColor = UIColor.blue
|
162
|
+
|
163
|
+
saveButton.backgroundColor = UIColor.red
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
//UILabelを表示する処理
|
172
|
+
|
173
|
+
@IBAction func actionCreateButton(_ sender: UIButton) {
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
let randX = Int(arc4random_uniform(300))
|
178
|
+
|
179
|
+
let randY = Int(arc4random_uniform(300))
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
testLabel.text = String("テスト")
|
184
|
+
|
185
|
+
testLabel.sizeToFit()
|
186
|
+
|
187
|
+
testLabel.center = CGPoint(x: randX, y: randY)
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
view.addSubview(testLabel)
|
192
|
+
|
193
|
+
print(testLabel.frame.origin.x)
|
194
|
+
|
195
|
+
print(testLabel.frame.origin.y)
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
//UILabelのプロパティを保存する処理
|
204
|
+
|
205
|
+
@IBAction func actionSaveButton(_ sender: UIButton) {
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
save.set(testLabel.center.x, forKey: "hoge")
|
210
|
+
|
211
|
+
save.set(testLabel.center.y, forKey: "fuga")
|
212
|
+
|
213
|
+
save.set(testLabel.text, forKey: "piyo")
|
214
|
+
|
215
|
+
save.synchronize()
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
//UIlabelのプロパティを出力する処理
|
224
|
+
|
225
|
+
@IBAction func actionLoadButton(_ sender: UIButton) {
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
testLabel.center = CGPoint(x: save.integer(forKey: "hoge"), y: save.integer(forKey: "fuga"))
|
230
|
+
|
231
|
+
testLabel.text = save.object(forKey: "piyo") as? String
|
232
|
+
|
233
|
+
testLabel.sizeToFit()
|
234
|
+
|
235
|
+
view.addSubview(testLabel)
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
```
|