###前提・実現したいこと
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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/01 08:43
2017/12/20 11:59