ページビューコントローラの管理するコンテンツのvcからBarButtonItemのボタン押下で別の
画面に遷移して、遷移先で設定などをして得た値を戻るときに渡して、その値によって遷移元を再描画(再読込)させたいのですが、遷移した際の状態のまま画面が表示されてしまいます。
解る方がいらっしゃいましたら教えて頂けないでしょうか。
宜しくお願い致します。
詳細説明
画面遷移はセグエを使用しています。
PageViewController(画像の左から4つ目のvc)に配置した「設定」ボタン(BarButtonItem)からSecondSettingViewController(画像下段のvc)へドラッグしてセグエをつないでいます。
■ 試したこと
PageViewcontrollerを取得してsoundSettingプロパティに値を渡そうとしていたのですが、コンテンツのvc(遷移元)には渡せていませんでした。PageViewControllerまでは値を渡せているようなのですが・・・
コンテンツのvc(遷移元)に直接戻っているように思ったのでPageContentViewControllerを取得して値を渡そうとしてみたのですが、vcの取得が間違っているのかうまくいきません。
該当のコード
// version Swift3 import UIKit class SecondSettingViewController: UIViewController { // シーン移動の際に渡される値を格納する変数 var soundSetting:Int = 1 var pageIndex:Int = 0 override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.lightGray // Viewを閉じる(遷移元に戻る)ボタン作成 let backVCButton = UIButton() backVCButton.frame = CGRect(x: 15, y: 20, width: 30, height: 30) backVCButton.setTitle("×", for: .normal) backVCButton.titleLabel?.font = UIFont.systemFont(ofSize: 30) backVCButton.setTitleColor(UIColor.gray, for: .normal) backVCButton.setTitleColor(UIColor.white, for: .highlighted) backVCButton.addTarget(self, action: #selector(backVC(sender:)), for: .touchUpInside) self.view.addSubview(backVCButton) // セグメントコントロール作成 let array : NSArray = ["OFF","ON"] let uiSegmentedControl: UISegmentedControl = UISegmentedControl(items: array as [AnyObject]) uiSegmentedControl.frame = CGRect(x: 0, y: 0, width: 200, height: 30) uiSegmentedControl.center = CGPoint(x: self.view.frame.width/2, y: 100) uiSegmentedControl.selectedSegmentIndex = 0 uiSegmentedControl.backgroundColor = UIColor.lightGray uiSegmentedControl.tintColor = UIColor.darkGray uiSegmentedControl.addTarget(self, action: #selector(self.segmentChanged(segcon:)), for: UIControlEvents.valueChanged) self.view.addSubview(uiSegmentedControl) } // viewDidLoadを閉じる // セグメントボタン押下時に実行するメソッド func segmentChanged(segcon: UISegmentedControl){ switch segcon.selectedSegmentIndex { case 0: soundSetting = 0 case 1: soundSetting = 1 default: print("Error") } } // Viewを閉じる(遷移元へ戻る)メソッド func backVC(sender: AnyObject) { //Navigation Controllerを取得 let nav = self.presentingViewController as! UINavigationController //遷移元のView Controllerを取得 let pageViewController = nav.topViewController as! PageViewController pageViewController.soundSetting = soundSetting /* // PageContentViewControllerを取得 ←このコードでは値を渡せませんでした。 let pcvc = pageViewController.contentVCs[pageIndex] as! PageContentViewController pcvc.soundSetting = soundSetting */ self.dismiss(animated: true, completion: nil) } }
その他のコード
import UIKit class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate { // シーン移動の際に渡される値を格納する変数 var selectedVC:Int = 0 var selectedSVC:Int = 0 var arrayLength = 0 // settingから渡される値を格納する変数 var soundSetting:Int = 3 // コンテンツに渡すページindex var pageIndex:Int = 0 // ページングするviewControllerを格納する配列 var contentVCs = [UIViewController]() // 設定画面に遷移するボタンのOutlet接続 @IBOutlet weak var settingButtonItem: UIBarButtonItem! (略) override func viewDidLoad() { super.viewDidLoad() dataSource = self delegate = self // コンテンツvcを作成 for index in 0..<arrayLength { let contentVC = storyboard?.instantiateViewController(withIdentifier: "PageContentViewController") as! PageContentViewController contentVC.pageIndex = index contentVC.soundSetting = soundSetting contentVCs.append(contentVC) } self.setViewControllers([contentVCs[selectedSVC]], direction: .forward, animated: true, completion: nil) } // viewDidLoadを閉じる // 設定ボタン押下でセグエ遷移 @IBAction func goSecondSetting(_ sender: Any) { performSegue(withIdentifier: "toSecondSetting", sender: nil) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let secondSettingVC = segue.destination as? SecondSettingViewController { secondSettingVC.pageIndex = currentIndex } } (略) } // classを閉じる
import UIKit import AVFoundation class PageContentViewController: UIViewController, UIScrollViewDelegate, AVAudioPlayerDelegate { // シーン移動の際に渡される値を格納するプロパティ var pageIndex:Int = 0 var selectedVC:Int = 0 var selectedSVC:Int = 0 // settingから預かるデータ var soundSetting:Int = 3 (略) override func viewDidLoad() { super.viewDidLoad() let label = UILabel() label.frame = CGRect(x: 100, y: 100 , width: 30, height: 30) label.text = "▶" label.textColor = UIColor.magenta if soundSetting == 1 { // 渡した値soundSettingを使用 scrollView.addSubview(label) } (略) } // viewDidLoadを閉じる (略) } // classを閉じる
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/23 11:25