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

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

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

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

Q&A

解決済

1回答

980閲覧

画面遷移ができない。 whose view is not in the window hierarchy!

atk_721

総合スコア62

Swift

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

0グッド

0クリップ

投稿2020/07/01 15:32

前提・実現したいこと

whose view is not in the window hierarchy!というエラーが発生しており、viewDidLoadで画面遷移を行うことで発生する様なのですが、遷移の処理はmainthreadで行っており、エラーの原因が特定できず困っています。

ConversationControllerのProfileControllerDelgateのhandleLogoutメソッド内でpresentLoginScreen()を呼ぶとエラーが発生し、呼ばなければ遷移せず、エラーは発生しませんでした。

profileControllerDelegate

発生している問題・エラーメッセージ

Warning: Attempt to present <UINavigationController: 0x10b867a00> on <FirebaseChat.conversationsViewController: 0x10a520990> whose view is not in the window hierarchy!

該当のソースコード

##問題のController

import Firebase import UIKit private let reuseIdentifier = "ConversationCell" class conversationsViewController: UIViewController { //MARK: - Property private let tableView = UITableView() private var conversations = [Conversation]() private let newMessageButton: UIButton = { let button = UIButton(type: .system) button.setImage(UIImage(systemName: "plus"), for: .normal) button.backgroundColor = .systemPurple button.tintColor = .white button.imageView?.setDimensions(height: 24, width: 24) button.addTarget(self, action: #selector(showNewMessage), for: .touchUpInside) return button }() //MARK: - View LifeCycle override func viewDidLoad() { super.viewDidLoad() configureUI() authenticateUser() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) fetchConversations() configureNavigationBar(Title: "Message", prefersLargeTitle: true) navigationController?.navigationBar.isHidden = false } //MARK: - Selector's @objc func showProfile() { let controller = ProfileController(style: .insetGrouped) //MARK: - Point TableViewに余白ができる。 controller.delegate = self let nav = UINavigationController(rootViewController: controller) nav.modalPresentationStyle = .fullScreen present(nav, animated: true, completion: nil) } @objc func showNewMessage() { //navigationBarを付与してから遷移 let controller = NewMessageController() controller.delegate = self let nav = UINavigationController(rootViewController: controller) nav.modalPresentationStyle = .fullScreen present(nav, animated: true, completion: nil) } //MARK: - API func fetchConversations() { Service.fetchConversation { (conversations) in self.conversations = conversations self.tableView.reloadData() } } func authenticateUser() { if Auth.auth().currentUser?.uid == nil { presentLoginScreen() }else{ print("DEBUG : current userId =>(Auth.auth().currentUser!.uid)") } } //MARK: - User Interface's private func configureUI() { // Style Property let bgColor: UIColor = .white view.backgroundColor = bgColor configureGradientLayer() configureNavigationBar(Title: "Message", prefersLargeTitle: true) configureTableView() navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "person.circle.fill"), style: .plain, target: self, action: #selector(self.showProfile)) view.addSubview(newMessageButton) newMessageButton.setDimensions(height: 56 ,width: 56) newMessageButton.layer.cornerRadius = 56/2 //この時点でbuttonのheightは0 ×:newMessageButton.frame.height newMessageButton.anchor(bottom: view.safeAreaLayoutGuide.bottomAnchor, right: view.rightAnchor, paddingBottom: 16, paddingRight: 24) } private func configureTableView() { //Style Property let tableViewBgColor: UIColor = .white tableView.register(ConversationCell.self, forCellReuseIdentifier: reuseIdentifier) tableView.backgroundColor = tableViewBgColor tableView.rowHeight = 80 tableView.tableFooterView = UIView() tableView.delegate = self tableView.dataSource = self tableView.frame = self.view.frame self.view.addSubview(self.tableView) } //MARK: - Helpers private func presentLoginScreen() { DispatchQueue.main.async { let controller = LoginController() let nav = UINavigationController(rootViewController: controller) nav.modalPresentationStyle = .fullScreen self.present(nav, animated: true, completion: nil) } } private func showChatController(foruser user: User) { let chatController = ChatController(user: user) self.navigationController?.pushViewController(chatController, animated: true) } } //MARK: - UITableviewDatasource extension conversationsViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return conversations.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! ConversationCell cell.conversation = conversations[indexPath.row] return cell } } //MARK: - UITableView Delegate extension conversationsViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) showChatController(foruser: conversations[indexPath.row].user) } } extension conversationsViewController: NewMessageControllerDelegate { func controller(_ controller: NewMessageController, wantsToStartWith user: User) { controller.dismiss(animated: true, completion: nil)// Point : NewMessageControllerからConvaseationsControllerに戻ってきてからpushViewController。 showChatController(foruser: user) } } //MARK: - ProfileController Delegate extension conversationsViewController: ProfileControllerDelegate { func handleLogout() { AuthService.shared.logout { (error) in if let error = error { self.showHud(error: true, title: "Logout Error", Description: error.localizedDescription) return } self.presentLoginScreen()//このメソッドを呼ぶとエラーが発生、遷移しない } } }

##AuthService Logout()

func logout(completion: @escaping(_ error: Error?) -> Void) { do{ try Auth.auth().signOut() completion(nil) }catch{ let error = CustomError.logout completion(error) } }

##ProfileDelegate HandleLogout()

protocol ProfileControllerDelegate: class { func handleLogout() }
//profileFooterViewのDelegateMethod名もhandleLogout ボタンが押された時に呼ばれるだけ。 extension ProfileController: ProfileFooterDelegate{ func handleLogout() { let alert = UIAlertController(title: nil, message: "Are you sure you want to logout ??", preferredStyle: .actionSheet) alert.addAction(UIAlertAction(title: "Log Out", style: .default, handler: { (_) in self.delegate?.handleLogout() // ProfileControllerDelegateのhandleLogout() })) alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) present(alert, animated: true, completion: nil) } }

試したこと

ConversationControllerのauthenticateUser()をViewdidappear()に変えたりしましたが、関係ありませんでした。

補足情報(FW/ツールのバージョンなど)

swift5
xcode 11

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

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

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

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

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

guest

回答1

0

自己解決

func handleLogout() { let alert = UIAlertController(title: nil, message: "Are you sure you want to logout ??", preferredStyle: .actionSheet) alert.addAction(UIAlertAction(title: "Log Out", style: .default, handler: { (_) in self.dismiss(animated: true) { self.delegate?.handleLogout() } })) alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) present(alert, animated: true, completion: nil) } }

delegateを委譲する前に,dismissを呼ぶと正常に動きました。

投稿2020/07/01 15:44

atk_721

総合スコア62

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問