質問編集履歴

3

追記

2018/05/29 10:28

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -37,3 +37,121 @@
37
37
  **コード等は下記のリポジトリにあげています。**
38
38
 
39
39
  https://github.com/haruka22/project
40
+
41
+
42
+
43
+ ### 追記
44
+
45
+ 遷移先のtableViewにデータが反映されない
46
+
47
+ ```HistoryViewController.swift
48
+
49
+ import UIKit
50
+
51
+
52
+
53
+ class HistoryViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
54
+
55
+
56
+
57
+ @IBOutlet weak var HistorytableView: UITableView!
58
+
59
+
60
+
61
+
62
+
63
+ var datetime = UILabel()//var datetime: UILabel! /オプショナル型はなるべく使わない
64
+
65
+ var nameitem = UILabel()
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+ // ボタンを用意
74
+
75
+ var addBtn: UIBarButtonItem!
76
+
77
+
78
+
79
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
80
+
81
+ return 0
82
+
83
+ }
84
+
85
+
86
+
87
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
88
+
89
+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath)
90
+
91
+ cell.textLabel?.text = datetime.text
92
+
93
+
94
+
95
+ return cell
96
+
97
+ }
98
+
99
+
100
+
101
+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
102
+
103
+ //セルの選択解除
104
+
105
+ tableView.deselectRow(at: indexPath, animated: true)
106
+
107
+
108
+
109
+ //ここに遷移処理を書く
110
+
111
+ self.present(ViewController(), animated: true, completion: nil)
112
+
113
+ }
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+ override func viewDidLoad() {
122
+
123
+ super.viewDidLoad()
124
+
125
+
126
+
127
+ HistorytableView.delegate = self
128
+
129
+ HistorytableView.dataSource = self
130
+
131
+ HistorytableView.backgroundColor = UIColor.lightGray
132
+
133
+ self.navigationItem.title = "データベース"
134
+
135
+ HistorytableView.reloadData()
136
+
137
+ }
138
+
139
+
140
+
141
+ func onClick() {
142
+
143
+ print("ボタンがおされました")
144
+
145
+ let second = ViewController()
146
+
147
+ present(second, animated: true, completion: nil)
148
+
149
+
150
+
151
+ }
152
+
153
+
154
+
155
+ }
156
+
157
+ ```

2

追記

2018/05/29 10:28

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,179 +1,39 @@
1
- **gitにプロジェクトをupしてます**
1
+ ### やりたこと
2
2
 
3
+ コードでの画面遷移を実現したい。(少し複雑な)
4
+
3
- https://github.com/haruka22/sample
5
+ ホーム画面で入力したしたデータを他のsceneに渡したい。
4
6
 
5
7
 
6
8
 
7
- **元々の遷移**
9
+ **出来ていること**
8
10
 
9
- ホーム画面(viewCotroller scene)の[遷移No.2]ボタンタップ時 → データの遷移先(tableView scene) → (label scene)
11
+ 入力したデータは他のscnenには渡せている。
10
12
 
11
- の流れでした
13
+ 基本的な画面遷移は出来ている
12
14
 
13
15
 
14
16
 
15
- ### やりた
17
+ **困ってること**
16
18
 
17
- 日記帳例えると
19
+ 他のscene渡したデータをtableViewに反映されない。
18
20
 
19
- label sceneは1日の出来事を記載する場所になります。
20
-
21
- そしてデータの遷移先(tableView scene)は2018/5/28や日記のタイトルとと言った目次のViewになります。
22
-
23
- やりたい事としては、遷移の回数を省略するためにデータの遷移先を経由しないでホーム画面(viewCotroller scene)からlabel scnenに遷移させたいです。その際に、ホーム画面をタップした時にアラートで入力した中身が(データの遷移先のタイトルになり、日付は遷移した時の日付になります。)
24
-
25
- やりたい事としては、カッコの部分を遷移せずに自動的に行いたいです。
26
-
27
- そして、日記帳なので振り返りたい時に以下の流れにしたいです。
28
-
29
- ホーム画面(viewCotroller scene)の[遷移No.2]ボタンタップ時 → データの遷移(tableView scene) → (label scene)
21
+ 下記の2.[History]ボタンからの遷移がうまく出来ずに困っている。(tableviewにデータを渡せれてないのが原因だと思われる。)
30
22
 
31
23
 
32
24
 
33
- **出来ている事と困っている事**
25
+ - 1.ホーム画面scene
34
26
 
35
- データを渡すことに関しては以前の質問などを参考にし稚拙なコードながら出来ていると思います。
27
+ - 2.内容scene
36
28
 
37
- 困っている事はやりたいことの中に書いていまが遷移全般が全く分からず、ポケットリファレンスやqiita等でも調べてみましたがそのような記事を見つけことができませんでした。
29
+ - 3.内容を管理するscene
38
30
 
39
- 下記のコードで遷移前にアラートを呼び出そうとしているのですが遷移後にアラートが呼び出される形になってしまい入力してからさらに遷移してしまう。
31
+  0. [start]ボタン 1 → 2
40
32
 
41
- ```swift
33
+ 0. [History]ボタン 1 → 3 → 2
42
34
 
43
- @IBAction func seguebtr1(_ sender: Any) {
44
35
 
45
- //ここではViewControllerに飛ぶ遷移についてと遷移先のnavigationbarのtitleの変更について
46
36
 
47
- let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
37
+ **コド等は下記のリポトリにあげています。**
48
38
 
49
-
50
-
51
- let okAction = UIAlertAction(title: "OK", style: .default, handler: {
52
-
53
- (action:UIAlertAction!) -> Void in
54
-
55
-
56
-
57
- if let textFields = alert.textFields {
58
-
59
-
60
-
61
- for textField in textFields {
62
-
63
- if let text = textField.text {
64
-
65
- print(textField.text!)
66
-
67
- //遅延に関するコード // セグエにtextを渡す
39
+ https://github.com/haruka22/project
68
-
69
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
70
-
71
- self.performSegue(withIdentifier: "ViewControllersegue", sender: text)//元々nilだった
72
-
73
- }
74
-
75
-
76
-
77
- }
78
-
79
- }
80
-
81
- }
82
-
83
- })
84
-
85
- alert.addAction(okAction)
86
-
87
-
88
-
89
- let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
90
-
91
- alert.addAction(cancelAction)
92
-
93
-
94
-
95
- alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
96
-
97
- textField.placeholder = "テキスト"
98
-
99
- })
100
-
101
- alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
102
-
103
- // アラートを画面に表示
104
-
105
- self.present(alert, animated: true, completion: nil)
106
-
107
-
108
-
109
- }
110
-
111
-
112
-
113
-
114
-
115
- @IBAction func seguebtr2(_ sender: Any) {
116
-
117
- }
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
126
-
127
- if segue.identifier == "ViewControllersegue",
128
-
129
- let nextView: label = segue.destination as? label,
130
-
131
- let text = sender as? String {
132
-
133
- nextView.titleName = text
134
-
135
- }
136
-
137
-
138
-
139
- if segue.identifier == "ViewControllersegue2",
140
-
141
- let nextView2: tableView = segue.destination as? tableView,
142
-
143
- let text = sender as? String {
144
-
145
- nextView2.titleName2 = text
146
-
147
-
148
-
149
- //subtitle 現在日時を取得
150
-
151
- let now = NSDate() // 現在日時の取得
152
-
153
-
154
-
155
- let dateFormatter = DateFormatter()
156
-
157
- dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale? // ロケールの設定
158
-
159
- dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm"//:ss" // 日付フォーマットの設定
160
-
161
-
162
-
163
- let dateString = dateFormatter.string(from: now as Date)
164
-
165
-
166
-
167
- nextView2.timeitem = dateString
168
-
169
- print(dateString) // -> 2014/06/25 02:13:18
170
-
171
- }
172
-
173
-
174
-
175
-
176
-
177
- }
178
-
179
- ```

1

追記

2018/05/29 08:44

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -35,3 +35,145 @@
35
35
  データを渡すことに関しては以前の質問などを参考にし稚拙なコードながら出来ていると思います。
36
36
 
37
37
  困っている事はやりたいことの中に書いていますが遷移全般が全く分からず、ポケットリファレンスやqiita等でも調べてみましたがそのような記事を見つけることができませんでした。
38
+
39
+ 下記のコードで遷移前にアラートを呼び出そうとしているのですが遷移後にアラートが呼び出される形になってしまい入力してからさらに遷移してしまう。
40
+
41
+ ```swift
42
+
43
+ @IBAction func seguebtr1(_ sender: Any) {
44
+
45
+ //ここではViewControllerに飛ぶ遷移についてと遷移先のnavigationbarのtitleの変更について
46
+
47
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
48
+
49
+
50
+
51
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
52
+
53
+ (action:UIAlertAction!) -> Void in
54
+
55
+
56
+
57
+ if let textFields = alert.textFields {
58
+
59
+
60
+
61
+ for textField in textFields {
62
+
63
+ if let text = textField.text {
64
+
65
+ print(textField.text!)
66
+
67
+ //遅延に関するコード // セグエにtextを渡す
68
+
69
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
70
+
71
+ self.performSegue(withIdentifier: "ViewControllersegue", sender: text)//元々nilだった
72
+
73
+ }
74
+
75
+
76
+
77
+ }
78
+
79
+ }
80
+
81
+ }
82
+
83
+ })
84
+
85
+ alert.addAction(okAction)
86
+
87
+
88
+
89
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
90
+
91
+ alert.addAction(cancelAction)
92
+
93
+
94
+
95
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
96
+
97
+ textField.placeholder = "テキスト"
98
+
99
+ })
100
+
101
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
102
+
103
+ // アラートを画面に表示
104
+
105
+ self.present(alert, animated: true, completion: nil)
106
+
107
+
108
+
109
+ }
110
+
111
+
112
+
113
+
114
+
115
+ @IBAction func seguebtr2(_ sender: Any) {
116
+
117
+ }
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
126
+
127
+ if segue.identifier == "ViewControllersegue",
128
+
129
+ let nextView: label = segue.destination as? label,
130
+
131
+ let text = sender as? String {
132
+
133
+ nextView.titleName = text
134
+
135
+ }
136
+
137
+
138
+
139
+ if segue.identifier == "ViewControllersegue2",
140
+
141
+ let nextView2: tableView = segue.destination as? tableView,
142
+
143
+ let text = sender as? String {
144
+
145
+ nextView2.titleName2 = text
146
+
147
+
148
+
149
+ //subtitle 現在日時を取得
150
+
151
+ let now = NSDate() // 現在日時の取得
152
+
153
+
154
+
155
+ let dateFormatter = DateFormatter()
156
+
157
+ dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale? // ロケールの設定
158
+
159
+ dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm"//:ss" // 日付フォーマットの設定
160
+
161
+
162
+
163
+ let dateString = dateFormatter.string(from: now as Date)
164
+
165
+
166
+
167
+ nextView2.timeitem = dateString
168
+
169
+ print(dateString) // -> 2014/06/25 02:13:18
170
+
171
+ }
172
+
173
+
174
+
175
+
176
+
177
+ }
178
+
179
+ ```