質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

269閲覧

view間での値の受け渡しについて

su_nodesi

総合スコア10

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/03/11 13:21

前提・実現したいこと

swiftで簡単な支出管理アプリを作成しています。
① Aの画面でtextfieldに支出を入力し、次の画面(B)に移るボタンを押す
② Bの画面に一定のルールで計算されたAの支出がtableviewに表示される。

ここで、Bの画面から再びAの画面に戻り、textfieldの内容を変えてBの画面に再度移ると
前回入力したtextfieldの内容が表示され、textfieldの変更が反映されない。

AからBに移り、BからAに戻った場合、textfieldの変更を反映させるには
どういった記述をすればよいでしょうか。

以下大変読み難いコードで恐縮ですが、ご教示いただけますと助かります。

Swift

1(Aページ) 2class APageViewController: UIViewController { 3 4 @IBOutlet weak var figure1: UITextField! 5 // 以下、figure8まであり、省略 6 7override func viewDidLoad() { 8 super.viewDidLoad() 9 } 10 11 @IBAction func gotoBPage(_ sender:UIButton) { 12 13 let next = storyboard!.instantiateViewController(withIdentifier: "BPage")as? BPageViewController 14 self.present(next!,animated: true, completion: nil) 15 16 next!.Bfigure1 = figure1.text! 17 // 以下、figure8まで同じ記述あり、省略 18 19 figure1.text = "" 20     // BPageに移る時にfigure1〜8を初期化 21 // 以下、figure8まで同じ記述あり、省略 22 23 } 24 25 override func didReceiveMemoryWarning() { 26 super.didReceiveMemoryWarning() 27 } 28 29 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 30 self.view.endEditing(true) 31 } 32} 33 34(Bページ) 35import UIKit 36 37// tableviewのcellに表示する要素をここに入力しています。省略。 38 39class BPageViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 40 41 var Bfigure1: String = "" 42 // 以下、Bfigure8まで同様の記述あり、省略 43 44 override func viewDidLoad() { 45 super.viewDidLoad() 46 47 let BPageTableview:UITableView! 48 let BPageTableView = UITableView(frame: view.frame, style: .grouped) 49 50 BPageTableView.delegate = self 51 BPageTableView.dataSource = self 52 view.addSubview(BPageTableView) 53 54 let button = UIButton(type: UIButton.ButtonType.system) 55 let screenWidth:CGFloat = view.frame.size.width 56 let screenHeight:CGFloat = view.frame.size.height 57 button.addTarget(self, action: #selector(buttonEvent(_:)), for: UIControl.Event.touchUpInside) 58 button.setTitle("戻る", for: UIControl.State.normal) 59 button.sizeToFit() 60 button.center = CGPoint(x: screenWidth/2, y: screenHeight/1.03) 61 self.view.addSubview(button) 62 } 63 64 @objc func buttonEvent(_ sender: UIButton) { 65 let before = storyboard!.instantiateViewController(withIdentifier: "APage")as? APageViewController 66 self.present(before!,animated: true, completion: nil) 67 68 Bfigure1 = "" 69 // APageに戻った際、Bfigure1〜8を初期化しています。 70     // 以下、Bfigure8まで同様の記述あり、省略。 71 } 72 73 override func viewWillAppear(_ animated: Bool) { 74 // ここでBfigure1〜8をInt型に変換し、計算を行っています。省略。 75 } 76 77 func numberOfSections(in tableView: UITableView) -> Int { 78 return SectionTitle.count 79 } 80 81 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 82 return SectionTitle[section] 83 } 84 85 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 86 return 8 87 } 88 89 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 90 let cell = UITableViewCell(style: .value2, reuseIdentifier: "cell") 91 let sectionData = StableData[(indexPath as NSIndexPath).section] 92 let cellData = sectionData[(indexPath as NSIndexPath).row] 93 cell.textLabel?.text = self.title1[indexPath.row] 94 cell.detailTextLabel?.text = " " + "(cellData)" + "円" 95 return cell 96 } 97} 98

試したこと

AページからBページに移る際にfigure1〜8を初期化し、
BページからAページに戻る際にBfigure1〜8を初期化しているため、
再度AページからBページに移った際に変更されたtextfieldの内容が反映されるのでは?と考えています。

また、Aページのtextfieldの変更内容を察知する機能が足りていないのでは?と考え、AページのtextfieldをeditingChangeでstoryboard上でaction接続し、以下のコードをAページのViewControllerクラス直下に入力しましたが、変更内容は反映されませんでした。

@IBAction func catchEvent1(_ sender: UITextField) {
let next = storyboard!.instantiateViewController(withIdentifier: "BPage")as? BPageViewController
next!.Bfigures1 = figure1.text!
}
//textfieldが編集された際にBページのBfigure1にAページのfigureの数値を格納する。

補足情報(FW/ツールのバージョンなど)

Xcode 10.1
Swift 4.2

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

t_obara

2019/03/12 01:33

Aに入力フィールド、Bに結果出力としているのであれば、何もしなければAの入力フィールドが勝手に変更されることはありません。Bから戻る際、あるいはAからBに遷移する際に入力フィールドを変更させるからおかしくなるのです。
MasakiHori

2019/03/12 03:27

そもそもBから元のAに戻らずに、新しく作成した`別の`Aに遷移してるように見えるけど
guest

回答1

0

BPageViewController.viewWillAppearがgotoBPageのnext.BfigureXに設定するよりも早く呼ばれてるのでは?
presentが呼ばれた時点でviewWillAppearは呼ばれていたかと。
presentをgotoBPageの最後に持ってきたらどうでしょうか?

Swift

1@IBAction func gotoBPage(_ sender:UIButton) { 2 3 let next = storyboard!.instantiateViewController(withIdentifier: "BPage")as? BPageViewController 4 5 next!.Bfigure1 = figure1.text! 67     8    // 全部設定が完了してから遷移開始 9 self.present(next!,animated: true, completion: nil) 10 }

投稿2019/03/11 16:13

toki_td

総合スコア2850

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問