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

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

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

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

3269閲覧

画面遷移でのデータの受け渡しがうまくできない

bacchi

総合スコア13

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

1クリップ

投稿2016/12/01 07:44

編集2016/12/01 08:18

###前提・実現したいこと
Swiftで、UITableViewのCellを選択した時に別のViewControllerに遷移し、その時選択したセル番号を表示させたいと思っています。

Cellを選択し画面遷移するところまではうまくいきましたが、セル番号がnillとなってしまいます。
どうすればnillにならずにきちんとセル番号が表示できるでしょうか?
アドバイスお願いします。

###AppDelegate.swift

Swift3.0

1import UIKit 2 3@UIApplicationMain 4class AppDelegate: UIResponder, UIApplicationDelegate { 5 6 var window: UIWindow? 7 8 var Number: Int? 9 10 11 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 12 // Override point for customization after application launch. 13 return true 14 } 15 16 func applicationWillResignActive(_ application: UIApplication) { 17 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 18 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 19 } 20 21 func applicationDidEnterBackground(_ application: UIApplication) { 22 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 23 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 24 } 25 26 func applicationWillEnterForeground(_ application: UIApplication) { 27 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 28 } 29 30 func applicationDidBecomeActive(_ application: UIApplication) { 31 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 32 } 33 34 func applicationWillTerminate(_ application: UIApplication) { 35 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 36 } 37 38 39}

###ViewController.swift(遷移前)

Swift3.0

1import UIKit 2 3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 private let myItems: NSArray = ["おうし座", "おおいぬ座", "おおぐま座"] 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 // Do any additional setup after loading the view, typically from a nib. 10 } 11 12 override func didReceiveMemoryWarning() { 13 super.didReceiveMemoryWarning() 14 // Dispose of any resources that can be recreated. 15 } 16 17 /* 18 Cellが選択された際に呼び出される 19 */ 20 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 21 print("Num: (indexPath.row)") 22 print("Value: (myItems[indexPath.row])") 23 24 let storyboard: UIStoryboard = self.storyboard! 25 let nextView = storyboard.instantiateViewController(withIdentifier: "second") as! SecondViewController 26 self.present(nextView, animated: true, completion: nil) 27 28 let Number = indexPath.row 29 30 let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate 31 appDelegate.Number = Number 32 } 33 34 /* 35 Cellの総数を返す. 36 */ 37 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 38 return myItems.count 39 } 40 41 /* 42 Cellに値を設定する 43 */ 44 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 45 // 再利用するCellを取得する. 46 let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath) 47 48 // Cellに値を設定する. 49 cell.textLabel!.text = "(myItems[indexPath.row])" 50 51 return cell 52 } 53}

###SecondViewController.swift(遷移後)

Swift3.0

1import UIKit 2 3class SecondViewController: UIViewController { 4 5 @IBOutlet weak var Label: UILabel! 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 // Do any additional setup after loading the view. 11 12 let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate 13 let Number = appDelegate.Number 14 15 Label.text = "(Number)" 16 } 17 18 override func didReceiveMemoryWarning() { 19 super.didReceiveMemoryWarning() 20 // Dispose of any resources that can be recreated. 21 } 22 23 /* 24 // MARK: - Navigation 25 26 // In a storyboard-based application, you will often want to do a little preparation before navigation 27 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 28 // Get the new view controller using segue.destinationViewController. 29 // Pass the selected object to the new view controller. 30 } 31 */ 32}

###補足情報(言語/FW/ツール等のバージョンなど)
Swift3.0
Xcode8.0

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

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

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

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

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

guest

回答1

0

ベストアンサー

presentの位置が悪いと思います。

swift

1 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 2 print("Num: \(indexPath.row)") 3 print("Value: \(myItems[indexPath.row])") 4 5 let storyboard: UIStoryboard = self.storyboard! 6 let nextView = storyboard.instantiateViewController(withIdentifier: "second") as! SecondViewController 7 // 後ろに移動 8 //self.present(nextView, animated: true, completion: nil) 9 10 let Number = indexPath.row 11 12 let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate 13 appDelegate.Number = Number 14 // ここに移動 15 self.present(nextView, animated: true, completion: nil) 16 17 } 18

おまけ

SecondViewにnumberプロパティを持たせたやり方
(1ファイル分(AppDelegate)影響範囲が狭まった)

swift

1 // ViewController 2 3 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 4 5 let storyboard: UIStoryboard = self.storyboard! 6 let nextView = storyboard.instantiateViewController(withIdentifier: "second") as! SecondViewController 7 8 nextView.number = indexPath.row 9 10 self.present(nextView, animated: true, completion: nil) 11 12 } 13

swift

1class SecondViewController: UIViewController { 2 3 @IBOutlet weak var Label: UILabel! 4 5 var number:Int? 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 // Do any additional setup after loading the view. 11 12 Label.text = "\(number)" 13 } 14 // 略 15} 16

しかし、サイトがクソ重いな...

投稿2016/12/01 08:15

編集2016/12/01 08:28
fromageblanc

総合スコア2724

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

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

bacchi

2016/12/01 08:43

おまけまでありがとうございます!
LFOHP

2017/12/20 11:59

とても勉強になります
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問