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

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

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

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

Q&A

解決済

2回答

2217閲覧

セルをタップした時にそのセルの情報をセグエした先のViewControllerで表示したいのですが

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

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

0グッド

0クリップ

投稿2015/07/10 05:08

現在swiftを使って簡単な日記アプリを作っています。
投稿された日記をUserDefaultsを使って保存し、一覧をTableViewのセルに表示するところまでは実装できたのですが、セルをタップした時に値がセグエ先のViewControllerに渡せていません。
AppDelegateを使って値を渡そうとすると、最初のタップがnilになり、次のタップの時に先ほどタップしたセルの情報がセグエ先のViewControllerに表示されてしまいます。
プロトコルを作成して値を渡したほうが良いのでしょうか?
また、セルの情報を別のViewControllerで表示する方法の主流は何なのでしょうか?

見づらいかもしれませんが、私が書いたコードも記述しておきます。

lang

1Appdelegate 2 3class AppDelegate: UIResponder, UIApplicationDelegate { 4 var diaryContent:String? 5 var diaryDate:String? 6}

lang

1//セルがタップされた時のメソッド 2 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 3 performSegueWithIdentifier("ToDetailDiaryViewController", sender: nil) 4 let diary = diaries[indexPath.row] 5 diaryContent = diary.content 6 diaryDate = diary.date 7 } 8override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 9 if segue.identifier == "ToDetailDiaryViewController" { 10 let destinationViewController = segue.destinationViewController as! UINavigationController 11 let detailDiryViewController = destinationViewController.topViewController as! DetailDiaryViewController 12 detailDiryViewController.diary = self.currentSelectedDiary 13 var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 14 appDelegate.diaryContent = diaryContent 15 appDelegate.diaryDate = diaryDate 16 } else { 17 let destinationViewController = segue.destinationViewController as! UINavigationController 18 let newDiaryViewController = destinationViewController.topViewController as! NewDiaryViewController 19 newDiaryViewController.delegate = self 20 } 21 }

lang

1var appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 2 var diaryContent = appDelegate.diaryContent 3 var diaryDate = appDelegate.diaryDate 4 5 diaryTextView.text = diaryContent 6 self.navigationItem.title = "\(diaryDate)" 7 println(diaryDate) 8 println(diaryContent)

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

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

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

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

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

Stripe

2015/07/10 12:28

「セグエ先のViewController」のコードも書いてください。
guest

回答2

0

ベストアンサー

ご呈示のコードを最小限で修正するなら、

lang

1 var diaryContent //メンバ変数 2 var diaryDate 3 4//セルがタップされた時のメソッド 5 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 6 let diary = diaries[indexPath.row] 7 self.diaryContent = diary.content 8 self.diaryDate = diary.date 9 performSegueWithIdentifier("ToDetailDiaryViewController", sender: nil) 10 } 11override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 12 if segue.identifier == "ToDetailDiaryViewController" { 13 let destinationViewController = segue.destinationViewController as! UINavigationController 14 let detailDiryViewController = destinationViewController.topViewController as! DetailDiaryViewController 15 detailDiryViewController.diary = self.currentSelectedDiary 16 detailDiryViewController.diaryContent = self.diaryContent //遷移先のコントローラーに格納用のプロパティを用意しておく 17 detailDiryViewController.diaryDate = self.diaryDate 18 } else { 19 let destinationViewController = segue.destinationViewController as! UINavigationController 20 let newDiaryViewController = destinationViewController.topViewController as! NewDiaryViewController 21 newDiaryViewController.delegate = self 22 } 23 }

こんな感じでしょうか。
手打ちなのでビルドが通らなければ申し訳無いですが、やりたい事はご推察いただけると思います。

投稿2015/07/11 13:06

terushu

総合スコア358

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

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

0

そのコードですと、prepareForSegueが呼ばれた時点では、appDelegateのメンバ変数にまだ値が入っていない状態になってしまいます。
早急にコードを直すとしたら

lang

1 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 2 let diary = diaries[indexPath.row] 3 var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 4 appDelegate.diaryContent = diary.content 5 appDelegate.diaryDate = diary.date 6 performSegueWithIdentifier("ToDetailDiaryViewController", sender: nil) 7 }

とすれば良いと思われますが、そもそもdiary~変数はappDelegateに定義せずとも、呼び出し元のメンバ変数にしておけば良いと思います。

投稿2015/07/10 14:20

編集2015/07/10 14:27
terushu

総合スコア358

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

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

退会済みユーザー

退会済みユーザー

2015/07/11 02:34

ご回答ありがとうございます! 返信が遅くなってしまい、すみません。 diary~変数を呼び出し元のメンバ変数にする場合は、どこに記述したらよいのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問