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

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

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

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

iOS

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

Swift

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

Q&A

解決済

2回答

354閲覧

tableviewからの画面遷移での多量データの値渡しの手段

LFOHP

総合スコア25

Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

iOS

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

Swift

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

0グッド

0クリップ

投稿2017/12/21 02:15

###前提・実現したいこと
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

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

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

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

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

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

guest

回答2

0

自己解決

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { //print("Num: (indexPath.row)") //print("Value: (myItems[indexPath.row])") num = Int(indexPath.row) vc.nu = num tableView.deselectRow(at: indexPath, animated: true) self.present(vc, animated: true, completion: nil)

投稿2017/12/22 06:39

LFOHP

総合スコア25

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

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

0

NextViewControllerに受け渡し用のプロパティを作って直接渡せばいいと思います。

投稿2017/12/21 02:23

fuzzball

総合スコア16731

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

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

LFOHP

2017/12/21 02:26

具体的に教示して頂けますと幸いです。
fuzzball

2017/12/21 02:41 編集

具体的に書いているつもりですが、どの部分が分からないのでしょうか?
LFOHP

2017/12/21 02:42

コードを書いてもらえると助かるのですが。「プロパティを作る」とはどうするのでしょうか?
fuzzball

2017/12/21 02:55

あなたがAppDelegateに書いている、 var num:Int? こういうのがプロパティです。
LFOHP

2017/12/21 03:10

App/Delegateのvar num:Int?を消して class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let num:Int = 0 class NextViewController: UIViewController { var num:Int? label.text = "Num:(String(describing: num)) " としてみましたが、nilになります
LFOHP

2017/12/21 03:18

受け渡し用のプロパティとは、どのように作るのでしょうか?
fuzzball

2017/12/21 03:47

受け渡し用のプロパティはそれでいいですが、そこに値を入れないといけません。 NextViewControllerのインスタンスに対して、プロパティに値を入れて下さい。
LFOHP

2017/12/21 04:10

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { var num:Int = 0 let second:NextViewController = NextViewController() func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { second.num = indexPath.row class NextViewController: UIViewController { let second:NextViewController = NextViewController() var num:Int? label.text = "Num:(String(describing:second.num)) " こんなエラーになるのですが、どこがいけないのでしょうか Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee0944ff8)
LFOHP

2017/12/21 08:02

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { var num :Int = 0 let vc:NextViewController = NextViewController() func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { num = indexPath.row vc.num = num //セルの選択解除 tableView.deselectRow(at: indexPath, animated: true) //ここに遷移処理を書く self.present(NextViewController(), animated: true, completion: nil) class NextViewController: UIViewController { var num:Int = 0 override func viewDidLoad() { super.viewDidLoad() label.text = " (String(describing:num))" これだと、 var num:Int = 0の0しかでてこないのですが、どこが間違っているのでしょうか
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問