我有一个Objective-C类。我的可可课程与我的Objective-C类之间的接口管理器。我将价值传递给Swift课程,一旦我在可可级课程中使用提交按钮结束会话。然后从我的接口类中,我使用通知中心去另一个VC。但是我的应用程序有一个标签栏。因此,每次我需要回家,然后去SessionVC。我不想展示我的家庭风险投资。我直接想显示我的sessionvc。
这是我的代码与回家VC一起工作,然后转到SessionVC:
更新代码:
@objc func onOver(notification: NSNotification)
{
if let score = notification.object as? NSDictionary
{
DispatchQueue.background(delay: 0.1000, completion:{
DispatchQueue.main.async {
let storyBoard = UIStoryboard.init(name: "Trai", bundle: nil);
if let sessionvc = storyBoard.instantiateViewController(withIdentifier:
"sessionvc") as? sessionvc{
if let navController = TopView.topViewController()?.navigationController as? UINavigationController{
TopView.topViewController()?.navigationController?.pushViewController(sessionvc, animated: true)
}else{
TopView.topViewController()?.present(sessionvc, animated: true)
}
}
}
})
}
}
您的情况实际上包含了三件事,这些内容是tabbrcontroller,可能是导航控制器,当然也可能是uiviewController,因此,当从非UiviewController类中发布通知时,您永远不会知道您是哪个控制器的信息使用0索引访问NAV控制器不会解决您的问题之后以确定顶级控制器。
//Put this in separate file
import UIKit
class Utility:NSObject{
class func topViewController(base: UIViewController? = UIApplication.sharedApplication.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
,然后而不是这样做
if let controller = self.window?.rootViewController as? TBTabBarController{
if let navController: UINavigationController = controller.viewControllers?[0] as? UINavigationController
{
做这个
let storyBoard = UIStoryboard.init(name: "TBTrain", bundle: nil);
if let sessionController = storyBoard.instantiateViewController(withIdentifier:
"Ssessionvc") as? Ssessionvc{
if let navController = Utility.topViewController()?.navigationController as? UINavigationController{ Utility.topViewController()?.navigationController?.pushViewController(sessionController, animated: true)
}
}
else{
Utility.topViewController()?.present(sessionController, animated: true)
}
**注意:**如果在塔巴尔(Tabbar(中没有任何导航控制器的情况下,则实际上需要呈现控制器而不是推送