以下のようにAppDelegateを設定すると、UIViewContorllerの上にUIViewContorllerを乗っけられるのですが、この状態で上のビュー(OverlayViewController)のボタンを押しても,下のビュー(ViewController)の関数が呼び出されません。
なぜでしょうか?
RecorgingButtonTapped---------
はprintされるのに、pushRecording---------
はprintされないという状況です。特にエラーが出るわけでもないのに、pushRecordingを通過しないので不思議です。
これらの画面の上に画面を乗せるのは、以下のApple公式のサンプルをもとに作成しています。
https://developer.apple.com/documentation/arkit/streaming_an_ar_experience
appDelegate.swift
import UIKit
///- Tag: AppDelegate
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var overlayWindow: UIWindow!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
guard let window = window,
let windowScene = window.windowScene else { fatalError() }
// Create a window for the overlay.
overlayWindow = UIWindow(windowScene: windowScene)
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let overlayViewController = storyBoard.instantiateViewController(
identifier: "OverlayViewController")
overlayWindow.rootViewController = overlayViewController
overlayWindow.makeKeyAndVisible()
// Make sure the overlayWindow is always above the main window.
overlayWindow.windowLevel = window.windowLevel + 1
// Make the overlayWindow transparent so that the main window remains visible underneath.
overlayWindow.backgroundColor = .clear
return true
}
}
ViewController.swift
import UIKit
import Foundation
class ViewController: UIViewController,ViewControllerDelegate {
func pushRecording()
{
print("pushRecording---------")
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
OverlayViewController().delegate = self // delegateを登録
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
OverlayViewController.swift
import UIKit
import Foundation
import ReplayKit
protocol ViewControllerDelegate: AnyObject {
func pushRecording()
}
class OverlayViewController: UIViewController {
var delegate: ViewControllerDelegate?
@IBAction func RecorgingButtonTapped(_ sender: Any)
{
print("RecorgingButtonTapped---------")
delegate?.pushRecording()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Main.storyboad