質問編集履歴

4

ボタンが反応しない画面のコードの追加

2019/01/17 02:44

投稿

mimamo
mimamo

スコア44

test CHANGED
File without changes
test CHANGED
@@ -64,6 +64,282 @@
64
64
 
65
65
 
66
66
 
67
+ 「印刷する」というボタンを配置したい画面のコードは以下のようになっています。
68
+
69
+
70
+
71
+ ```Swift
72
+
73
+ import UIKit
74
+
75
+ import PDFKit
76
+
77
+
78
+
79
+ class tenkaizu: UIViewController {
80
+
81
+
82
+
83
+ var hutotappedBtnTag: Int?
84
+
85
+ @IBOutlet weak var pdfView: PDFView!
86
+
87
+
88
+
89
+ let hoge = UserDefaults.standard.integer(forKey: "selectedTag")
90
+
91
+
92
+
93
+ var path:String = ""
94
+
95
+
96
+
97
+ override func viewDidLoad() {
98
+
99
+ super.viewDidLoad()
100
+
101
+
102
+
103
+ let pdfView = PDFView(frame: view.frame)
104
+
105
+
106
+
107
+ // tagを取り出す場所に(hogeに格納してます)
108
+
109
+ let hoge = UserDefaults.standard.integer(forKey: "selectedTag")
110
+
111
+
112
+
113
+
114
+
115
+ // 取り合えずプリント、値が渡ってきていなければnil
116
+
117
+ //print("### hoge:", hoge as Any)
118
+
119
+
120
+
121
+ //tenkaiImage.image = hu1
122
+
123
+ switch hoge { //今はそのまま選択した画像を表示してるけど、封筒の裏表のプレビュー的な感じにする
124
+
125
+ case 1:
126
+
127
+ print("展開1")
128
+
129
+
130
+
131
+ // 読み込み
132
+
133
+ // PDFDocument
134
+
135
+ let path: String = Bundle.main.path(forResource: "huto1", ofType: "pdf")!
136
+
137
+ print("取得")
138
+
139
+ print(path)
140
+
141
+ let pdfURL = URL(fileURLWithPath: path)
142
+
143
+ let document = PDFDocument(url: pdfURL)
144
+
145
+ pdfView.document = document
146
+
147
+
148
+
149
+
150
+
151
+ // PDFの拡大率を調整する
152
+
153
+ pdfView.autoScales = true
154
+
155
+ // 表示モード
156
+
157
+ pdfView.displayMode = .singlePageContinuous
158
+
159
+
160
+
161
+ view.addSubview(pdfView)
162
+
163
+
164
+
165
+ UserDefaults.standard.set(path, forKey:"Hutopath")
166
+
167
+
168
+
169
+ case 2:
170
+
171
+ print("展開2")
172
+
173
+ let path: String = Bundle.main.path(forResource: "huto2", ofType: "pdf")!
174
+
175
+ print("取得")
176
+
177
+ print(path)
178
+
179
+ let pdfURL = URL(fileURLWithPath: path)
180
+
181
+ let document = PDFDocument(url: pdfURL)
182
+
183
+ pdfView.document = document
184
+
185
+
186
+
187
+
188
+
189
+ // PDFの拡大率を調整する
190
+
191
+ pdfView.autoScales = true
192
+
193
+ // 表示モード
194
+
195
+ pdfView.displayMode = .singlePageContinuous
196
+
197
+
198
+
199
+ view.addSubview(pdfView)
200
+
201
+
202
+
203
+ UserDefaults.standard.set(path, forKey:"Hutopath")
204
+
205
+
206
+
207
+ default:break
208
+
209
+ }
210
+
211
+
212
+
213
+ }
214
+
215
+
216
+
217
+ @IBAction func letsPrint(_ sender: UIButton) {
218
+
219
+ self.showPrinterPicker()
220
+
221
+ }
222
+
223
+
224
+
225
+ func showPrinterPicker() {
226
+
227
+ // UIPrinterPickerControllerのインスタンス化
228
+
229
+ let printerPicker = UIPrinterPickerController(initiallySelectedPrinter: nil)
230
+
231
+
232
+
233
+ // UIPrinterPickerControllerをモーダル表示する
234
+
235
+ printerPicker.present(animated: true, completionHandler:
236
+
237
+ {
238
+
239
+ [unowned self] printerPickerController, userDidSelect, error in
240
+
241
+ if (error != nil) {
242
+
243
+ // エラー
244
+
245
+ print("Error : (String(describing: error))")
246
+
247
+ } else {
248
+
249
+ // 選択したUIPrinterを取得する
250
+
251
+ if let printer: UIPrinter = printerPickerController.selectedPrinter {
252
+
253
+ print("Printer's URL : (printer.url)")
254
+
255
+ self.printToPrinter(printer: printer)
256
+
257
+ } else {
258
+
259
+ print("Printer is not selected")
260
+
261
+ }
262
+
263
+ }
264
+
265
+ }
266
+
267
+ )
268
+
269
+ }
270
+
271
+
272
+
273
+ func printToPrinter(printer: UIPrinter) {
274
+
275
+
276
+
277
+ print("印刷するぜい")
278
+
279
+ let HutoPath = UserDefaults.standard.string(forKey:"Hutopath")
280
+
281
+ print(HutoPath!)
282
+
283
+ let pdfURL = URL(fileURLWithPath: HutoPath!)
284
+
285
+
286
+
287
+ // 印刷してみる
288
+
289
+ let printIntaractionController = UIPrintInteractionController.shared
290
+
291
+ let info = UIPrintInfo(dictionary: nil)
292
+
293
+ info.jobName = "HutoSample Print"
294
+
295
+ info.orientation = .portrait
296
+
297
+ printIntaractionController.printInfo = info
298
+
299
+ //印刷する内容
300
+
301
+ printIntaractionController.printingItem = pdfURL
302
+
303
+ printIntaractionController.print(to: printer, completionHandler: {
304
+
305
+ controller, completed, error in
306
+
307
+ })
308
+
309
+ }
310
+
311
+
312
+
313
+
314
+
315
+
316
+
317
+ override func didReceiveMemoryWarning() {
318
+
319
+ super.didReceiveMemoryWarning()
320
+
321
+ }
322
+
323
+
324
+
325
+
326
+
327
+
328
+
329
+ }
330
+
331
+
332
+
333
+
334
+
335
+
336
+
337
+ ```
338
+
339
+
340
+
341
+
342
+
67
343
  ご教授よろしくお願いいたします。
68
344
 
69
345
 

3

画像の変更

2019/01/17 02:44

投稿

mimamo
mimamo

スコア44

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
  Navigationbarを2つ、このようにstoryboard上で設定しています。
40
40
 
41
- ![イメージ説明](e1521a26c23aa605856cee83210dd582.png)
41
+ ![イメージ説明](2bdacac7777dd11456a21095980fd6d6.png)
42
42
 
43
43
  上側の遷移だとtoolbarは使えるのですが、下側の遷移はtoolbarがうまく使えなくなっています。
44
44
 

2

説明の付け加え

2019/01/16 12:38

投稿

mimamo
mimamo

スコア44

test CHANGED
File without changes
test CHANGED
@@ -36,6 +36,14 @@
36
36
 
37
37
 
38
38
 
39
+ Navigationbarを2つ、このようにstoryboard上で設定しています。
40
+
41
+ ![イメージ説明](e1521a26c23aa605856cee83210dd582.png)
42
+
43
+ 上側の遷移だとtoolbarは使えるのですが、下側の遷移はtoolbarがうまく使えなくなっています。
44
+
45
+
46
+
39
47
  また、ボタンで起こしたいactionは
40
48
 
41
49
 

1

本文の変更

2019/01/16 10:28

投稿

mimamo
mimamo

スコア44

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,6 @@
1
1
  ### 前提・実現したいこと
2
+
3
+
2
4
 
3
5
 
4
6
 
@@ -9,6 +11,50 @@
9
11
  この画像のようにtoolbarを設置して、「印刷する」というボタンが使えるようにしたいのですが、
10
12
 
11
13
  ボタンが全く反応しなくて困っています。
14
+
15
+
16
+
17
+ ### 該当箇所
18
+
19
+
20
+
21
+ UIパーツはstoryboardで設置しています。
22
+
23
+ また、Navigation Controllerを前の方に設置しています。
24
+
25
+ 「トップへ」というボタンは問題なく使えています。
26
+
27
+
28
+
29
+ ![イメージ説明](c7823a4b176415e29832299958331fd8.png)
30
+
31
+ ![イメージ説明](cc3e97a646a99e9966977d2666fb4eab.png)
32
+
33
+
34
+
35
+ 上の画像のように、UIToolbarを使い、toolbar上のItemに「印刷する」というタイトルをつけています。storyboardでは「印刷する」という文字が見えなくなっていますが実行すると文字は読めるようになっています。しかし、そのボタンを押した感覚はなく、反応も全くありません。
36
+
37
+
38
+
39
+ また、ボタンで起こしたいactionは
40
+
41
+
42
+
43
+ ```swift
44
+
45
+ @IBAction func letsprint(_ sender: UIBarButtonItem) {
46
+
47
+ self.showPrinterPicker()
48
+
49
+ }
50
+
51
+ ```
52
+
53
+ で繋げています。
54
+
55
+
56
+
57
+
12
58
 
13
59
  ご教授よろしくお願いいたします。
14
60