在 iOS 中导航回特定屏幕时出现问题
Issue while navigation back to specific screen in iOS
我有一个有点复杂的场景来调用多个屏幕并返回到我的应用程序中的特定屏幕 UI。我有一个 homeVC,当用户单击 homeVC 时,它会嵌入导航控制器中,它会显示另一个名为 detailVC 的屏幕。这是调用屏幕的层次结构
在 LastVC 中提交 api 后,我想直接导航到 DetailVC 屏幕,我尝试了多种方法,但我的应用程序卡住了。我试过了,
Unwind Segue
通过这段代码,我导航到 HomeVC,但它也卡住了,
self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
主要问题是应用程序卡住了,没有任何操作。我怎样才能做到这一点我已经尝试了很多解决方案但未能完成。
当我检查你的应用程序流程然后你呈现两个视图控制器时,主页呈现详细视图和详细视图呈现解释 vc。
但是当您尝试在任何已经呈现的视图上呈现另一个视图时 viewController 那么您可能会出错。
让我给出一些逻辑。了解以下代码并在您的应用中使用。也许对你有帮助。
主视图控制器:
Override func ViewDidLoad()
{
//Add Obeserver for presenting ExplainVC.
NotificationCenter.default.addObserver(self,selector: #selector(PresentExplainVC),name: NSNotification.Name(rawValue: “presentexplainvc”),object: nil)
//Add Obeserver for presenting DetailVC.
NotificationCenter.default.addObserver(self,selector: #selector(PresentDetailVC),name: NSNotification.Name(rawValue: “presentdetailvc”),object: nil)
}
@objc func PresentExplainVC()
{
//write code for presenting your ExplainVC
}
@objc func PresentDetailVC()
{
//write code for presenting your DetailVC
}
DetailVC:
当你展示 explainVC 然后使用下面的代码
self.navigationController?.dismiss(animated: true, completion: {
NotificationCenter.default.post(name: Notification.Name("presentexplainvc"), object: nil)
})
解释VC:
Override func ViewDidLoad()
{
NotificationCenter.default.addObserver(self,selector: #selector(PresentDetailVCFromHome),name: NSNotification.Name(rawValue: “presentdetailvcfromhome”),object: nil)
}
@objc func PresentDetailVCFromHome()
{
self.navigationController?.dismiss(animated: true, completion: {
NotificationCenter.default.post(name: Notification.Name("presentdetailvc"), object: nil)
})
}
VC1:(无论其列表或形式如何)
Override func ViewDidLoad()
{
NotificationCenter.default.addObserver(self,selector: #selector(PopviewtoexplainVC),name: NSNotification.Name(rawValue: “popviewtoexplainvc”),object: nil)
}
@objc func PopviewtoexplainVC()
{
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
NotificationCenter.default.post(name: Notification.Name("presentdetailvcfromhome"), object: nil)
})
self.navigationController?.popViewController(animated: false)
}
VC2:(无论其列表或形式)
Override func ViewDidLoad()
{
NotificationCenter.default.addObserver(self,selector: #selector(PopviewtoVC1),name: NSNotification.Name(rawValue: “popviewtovc1”),object: nil)
}
@objc func PopviewtoVC1()
{
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
NotificationCenter.default.post(name: Notification.Name("popviewtoexplainvc"), object: nil)
})
self.navigationController?.popViewController(animated: false)
}
上次VC:
使用下面的代码弹出到 DetailVC
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
NotificationCenter.default.post(name: Notification.Name("popviewtovc1"), object: nil)
})
self.navigationController?.popViewController(animated: false)
我有一个有点复杂的场景来调用多个屏幕并返回到我的应用程序中的特定屏幕 UI。我有一个 homeVC,当用户单击 homeVC 时,它会嵌入导航控制器中,它会显示另一个名为 detailVC 的屏幕。这是调用屏幕的层次结构
在 LastVC 中提交 api 后,我想直接导航到 DetailVC 屏幕,我尝试了多种方法,但我的应用程序卡住了。我试过了,
Unwind Segue
通过这段代码,我导航到 HomeVC,但它也卡住了,
self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
主要问题是应用程序卡住了,没有任何操作。我怎样才能做到这一点我已经尝试了很多解决方案但未能完成。
当我检查你的应用程序流程然后你呈现两个视图控制器时,主页呈现详细视图和详细视图呈现解释 vc。
但是当您尝试在任何已经呈现的视图上呈现另一个视图时 viewController 那么您可能会出错。
让我给出一些逻辑。了解以下代码并在您的应用中使用。也许对你有帮助。
主视图控制器:
Override func ViewDidLoad()
{
//Add Obeserver for presenting ExplainVC.
NotificationCenter.default.addObserver(self,selector: #selector(PresentExplainVC),name: NSNotification.Name(rawValue: “presentexplainvc”),object: nil)
//Add Obeserver for presenting DetailVC.
NotificationCenter.default.addObserver(self,selector: #selector(PresentDetailVC),name: NSNotification.Name(rawValue: “presentdetailvc”),object: nil)
}
@objc func PresentExplainVC()
{
//write code for presenting your ExplainVC
}
@objc func PresentDetailVC()
{
//write code for presenting your DetailVC
}
DetailVC:
当你展示 explainVC 然后使用下面的代码
self.navigationController?.dismiss(animated: true, completion: {
NotificationCenter.default.post(name: Notification.Name("presentexplainvc"), object: nil)
})
解释VC:
Override func ViewDidLoad()
{
NotificationCenter.default.addObserver(self,selector: #selector(PresentDetailVCFromHome),name: NSNotification.Name(rawValue: “presentdetailvcfromhome”),object: nil)
}
@objc func PresentDetailVCFromHome()
{
self.navigationController?.dismiss(animated: true, completion: {
NotificationCenter.default.post(name: Notification.Name("presentdetailvc"), object: nil)
})
}
VC1:(无论其列表或形式如何)
Override func ViewDidLoad()
{
NotificationCenter.default.addObserver(self,selector: #selector(PopviewtoexplainVC),name: NSNotification.Name(rawValue: “popviewtoexplainvc”),object: nil)
}
@objc func PopviewtoexplainVC()
{
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
NotificationCenter.default.post(name: Notification.Name("presentdetailvcfromhome"), object: nil)
})
self.navigationController?.popViewController(animated: false)
}
VC2:(无论其列表或形式)
Override func ViewDidLoad()
{
NotificationCenter.default.addObserver(self,selector: #selector(PopviewtoVC1),name: NSNotification.Name(rawValue: “popviewtovc1”),object: nil)
}
@objc func PopviewtoVC1()
{
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
NotificationCenter.default.post(name: Notification.Name("popviewtoexplainvc"), object: nil)
})
self.navigationController?.popViewController(animated: false)
}
上次VC:
使用下面的代码弹出到 DetailVC
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
NotificationCenter.default.post(name: Notification.Name("popviewtovc1"), object: nil)
})
self.navigationController?.popViewController(animated: false)