###前提・実現したいこと
NextViewControllerにTableViewからの値渡しをしたいのですが、現時点ではAppDelegateを使ってindexPath.rowは取れています。
クイズアプリを想定していますが、容易に更新可能な多くのデータを受け渡しするとなると・・・
(1)すべてAppDelegateに書き込む。
(2)csvファイルなどを作ってAppDelegate.swift読み込ませる
(3)その他の方法
などが思い浮かびますが、上級者の皆さんのスタンダードな方法はどれでしょうか?
(2)(3)の方法を教えてもらえると勉強になります。
###発生している問題・エラーメッセージ
エラーメッセージ でていません。下記でindexPath.rowは受け渡しできています
###該当のソースコード
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var num:Int? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true }
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let appDelegate = UIApplication.shared.delegate as! AppDelegate private let myItems: NSArray = ["TEST1", "TEST2", "TEST3","TEST4"] private var myTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height let displayWidth: CGFloat = self.view.frame.width let displayHeight: CGFloat = self.view.frame.height myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight)) myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell") myTableView.dataSource = self myTableView.delegate = self self.view.addSubview(myTableView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("Num: (indexPath.row)") print("Value: (myItems[indexPath.row])") tableView.deselectRow(at: indexPath, animated: true) appDelegate.num = indexPath.row self.present(NextViewController(), animated: true, completion: nil) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return myItems.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath) cell.textLabel!.text = "(myItems[indexPath.row])" return cell } }
class NextViewController: UIViewController { var label :UILabel = UILabel() let backButton:UIButton = UIButton() let appDelegate = UIApplication.shared.delegate as! AppDelegate override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.green label.frame = CGRect(x: self.view.frame.width/2-150, y: self.view.frame.height/2, width: 300, height: 60) label.textAlignment = .center label.textColor = UIColor.white label.text = "Num:(String(describing: appDelegate.num!)) " label.font = UIFont.boldSystemFont(ofSize: 30) label.adjustsFontSizeToFitWidth = true label.layer.masksToBounds = true label.layer.cornerRadius = 5.0 view.addSubview(label) backButton.frame = CGRect(x: 0, y: 0, width: 100, height: 100) backButton.setTitle("back", for: .normal) backButton.setTitleColor(UIColor.blue, for: .normal) backButton.layer.position = CGPoint(x: self.view.frame.width/2, y: self.view.frame.height/2+100) backButton.addTarget(self, action: #selector(back), for: .touchUpInside) self.view.addSubview(backButton) } @objc func back() { self.dismiss(animated: true, completion: nil) } // Do any additional setup after loading the view. override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
###試したこと
###補足情報(言語/FW/ツール等のバージョンなど)
Swift4
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。