admin管理员组文章数量:1336632
I have first view controller with navigation bar. Next I go to second view controller with navigation bar. And next I want to go to third view controller with navigation bar and overCurrentContext
options. And in third view controller I want to my back button return to first view controller. I use this code to do it:
In second vc:
let detailViewController = ThirdVC()
self.navigationController?.modalPresentationStyle = .overFullScreen
self.navigationController?.pushViewController(detailViewController, animated: false)
In third vc for back button:
self.navigationController?.popToRootViewController(animated: false)
But I can't see second vc as a background of third vc. .overCurrentContext
doesn't work. I see gray background. How to fix it?
I have first view controller with navigation bar. Next I go to second view controller with navigation bar. And next I want to go to third view controller with navigation bar and overCurrentContext
options. And in third view controller I want to my back button return to first view controller. I use this code to do it:
In second vc:
let detailViewController = ThirdVC()
self.navigationController?.modalPresentationStyle = .overFullScreen
self.navigationController?.pushViewController(detailViewController, animated: false)
In third vc for back button:
self.navigationController?.popToRootViewController(animated: false)
But I can't see second vc as a background of third vc. .overCurrentContext
doesn't work. I see gray background. How to fix it?
1 Answer
Reset to default 1.overFullScreen is a modalPresentationStyle, so It only work when you "present" the view modally. So it won't work with push, which push the ViewController into the navigation's stack.
In your case, if you really want to present the thirdVC modally and then pop to the rootViewController from secondVC as the secondVC has been push to the navigation's stack. You can achieve that by using delegate pattern to notify the secondVC to pop to re rootVC.
Another way is using closure as below:
In your thirdVC, declare a closure:
var onPopToRoot:(()-> Void)?
then in your pop function, for example:
@objc private func popToRoot() {
onPopToRoot?()
dismiss(animated: true)
}
In your secondVC:
@objc private func goThird() {
let detailViewController = ThirdVC()
self.navigationController?.modalPresentationStyle = .overFullScreen
detailViewController.onPopToRoot = {[weak self] in
self?.navigationController?.popToRootViewController(animated: true)
}
self.present(detailViewController, animated: true, completion: nil)
}
本文标签: iosHow to move between view controllersStack Overflow
版权声明:本文标题:ios - How to move between view controllers? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742414441a2470434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
modalPresentationStyle
is not used when a VC is pushed, only when presented. – HangarRash Commented Nov 19, 2024 at 19:57