Xcode Version 10.3
swift4
お世話になります。
ViewControllerにModalViewを表示させ、ModalView上のボタンから、他のviewに遷移させたいのですが、「whose view is not in the window hierarchy!」で遷移できません。
。viewの親子関係と言いますか、それが理解できておりません。解決策をご教示いただければ幸いです
swift
1// ViewController.swift 2import UIKit 3 4class ViewController: UIViewController, UIPopoverPresentationControllerDelegate { 5 @IBOutlet weak var btn: UIButton! 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 } 10 11 @IBAction func btnAction(_ sender: UIButton) { 12 // modalViewの表示 13 definesPresentationContext = true 14 let modalVC = ModalViewController() 15 modalVC.modalPresentationStyle = .popover 16 modalVC.preferredContentSize = CGSize(width: 200, height: 200) 17 modalVC.popoverPresentationController?.sourceView = view 18 modalVC.popoverPresentationController?.sourceRect = sender.frame 19 modalVC.popoverPresentationController?.permittedArrowDirections = .down 20 modalVC.popoverPresentationController?.delegate = self 21 present(modalVC, animated: true, completion: nil) 22 } 23 24 func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle { 25 return .none 26 } 27 28}
swift
1// ModalViewController.swift 2import UIKit 3 4class ModalViewController: UIViewController { 5 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 self.view.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5) 9 } 10 override func viewDidAppear(_ animated: Bool) { 11 let toDoBtn = UIButton() 12 toDoBtn.frame = CGRect(x:50, y: 50, width: 100, height: 50) 13 toDoBtn.setTitle("Next", for: .normal) 14 toDoBtn.setTitleColor(UIColor.black, for: .normal) 15 toDoBtn.addTarget(self, action: #selector(moveNext(_:)), for: .touchUpInside) 16 self.view.addSubview(toDoBtn) 17 } 18 19 @objc func moveNext(_ sender: UIButton) { 20 self.dismiss(animated: false, completion: nil) 21 DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { 22 let second = NextViewController() 23 self.present(second, animated: true, completion: nil) 24 } 25 26 } 27} 28
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/29 08:31