我有两个视图,V1和V2。我想"介绍" V2在V1上按下添加按钮时,"弹出"按下停止按钮时V2关闭,因此原始V1是堆栈的顶部。
根据我的阅读,我需要一个单独的V2视图控制器。根据我能找到的有限信息,我需要V1的视图控制器符合V2的协议V2delegate。这就是我所拥有的,但它不起作用:
ViewController1 with V1
class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout, FormViewControllerDelegate {
let form = FormViewController()
func addTapped() {
form.delegate = self
let nav = UINavigationController(rootViewController: form)
navigationController?.present(nav, animated: true)
}
func popForm() {
navigationController?.popViewController(animated: true)
navigationController?.popToViewController(self, animated: true)
print("popped")
}
}
ViewController2 with V2
class FormViewController: UIViewController {
var delegate: FormViewControllerDelegate?
func stopTapped() {
print("pop it")
delegate?.popForm()
}
}
protocol FormViewControllerDelegate {
func popForm()
}
我在这里做错了什么?
答案 0 :(得分:4)
在VC2中,更改为使用此代码
func stopTapped() {
print("pop it")
self.dismiss(animated: true, completion: nil)
}
答案 1 :(得分:2)
在ViewController1中使用它来呈现FormViewController
func addTapped() {
let nav = UINavigationController(rootViewController: form)
self.present(nav, animated: true)
}
在FormViewController中想要关闭时使用此
func stopTapped() {
self.dismiss(animated: true)
}
答案 2 :(得分:1)
您已经提示ViewController
未推送ViewController
,因此您需要的是dismiss
控制器,而不是来自导航堆栈的pop
控制器。
func popForm() {
navigationController?.dismiss(animated: true)
print("popped")
}
如果您将方法名称重命名为dissmissForm
而不是popForm
,则更好。
答案 3 :(得分:1)
你需要解雇VC2而不是pop。在自class如下:
func stopTapped() {
self.dismiss(animated: true, completion: { _ in })
}
答案 4 :(得分:1)
当您呈现任何视图控制器时,您必须使用dismissViewController方法来移除呈现的视图控制器。当您推送任何viewcontroller时使用popViewController。
答案 5 :(得分:1)
当您使用present
时,您必须使用dismiss
删除当前在堆栈中显示的类,当您dismiss
时,您刚才的下一个课程将位于堆。多数民众赞成...希望,它可能对你有帮助。
答案 6 :(得分:1)
弹出一个UIViewController
你需要推送而不是现在。如果您需要提供UIViewVontroller
,请点击" X"你需要解雇那个viewController。
用于推视图控制器
func addTapped() {
self.navigationController?.pushViewController(from, animated: true)
}
func stopTapped() {
self.navigationController?.popViewController(animated: true)
}
用于呈现视图控制器
func addTapped() {
self.present(from, animated: true, completion: nil)
}
func stopTapped() {
self.dismiss(animated: true, completion: nil)
}
您不需要为推送或展示UIViewController
的任何协议编码