Ios 如何检测容器嵌入UINavigationController何时显示新视图
我在情节提要中创建了UIViewController(A),并使用UIAVController添加了容器 我的嵌入式UINavController的根是带有按钮的UIViewController(B) 如果我点击按钮,NavController会显示一个新的UIViewController(C)Ios 如何检测容器嵌入UINavigationController何时显示新视图,ios,swift,xcode,Ios,Swift,Xcode,我在情节提要中创建了UIViewController(A),并使用UIAVController添加了容器 我的嵌入式UINavController的根是带有按钮的UIViewController(B) 如果我点击按钮,NavController会显示一个新的UIViewController(C) 所以我有UINavController->B->C。A-controller如何在它包含的NavController中检测B->C转换?有很多方法可以做到这一点,但最简单的解决方案可能是使用Notifi
所以我有UINavController->B->C。A-controller如何在它包含的NavController中检测B->C转换?有很多方法可以做到这一点,但最简单的解决方案可能是使用NotificationCenter。无论何时发布通知,都要侦听通知,为其指定一个唯一的名称,如ViewA或ViewB通知,然后侦听具有该名称的通知。检查以下示例:-
import UIKit
struct NotificationNames {
static let bViewController
static let cViewController
}
class AViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Listen for notification of from BViewcontroller
NotificationCenter.default.addObserver(self, selector: #selector(didLoadBViewController), name: Notification.Name(NotificationNames.bViewController), object: nil)
// Listen for notification of from CViewcontroller
NotificationCenter.default.addObserver(self, selector: #selector(didLoadCViewController), name: Notification.Name(NotificationNames.cViewController), object: nil)
}
@objc private func didLoadBViewController() {
print("B ViewController is loaded.")
}
@objc private func didLoadCViewController() {
print("C ViewController is loaded.")
}
// Remove notification listeners to save some memory
deinit {
NotificationCenter.default.removeObserver(self, name: NotificationNames.bViewController, object: nil)
NotificationCenter.default.removeObserver(self, name: NotificationNames.cViewController, object: nil)
}
}
class BViewcontroller: UIViewController {
// Post notification that B ViewController loaded.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
NotificationCenter.default.post(name: NotificationNames.bViewController, object: nil)
}
}
class CViewController: UIViewController {
// Post notification that C ViewController loaded.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
NotificationCenter.default.post(name: NotificationNames.cViewController, object: nil)
}
}
在UIViewController(B)中制定协议,并从UIViewController(a)中确认。然后,当您点击UIViewController(B)上的按钮时,从UIViewController(B)调用委托函数