我有一个HomeViewcontroller
,其中有一个分段控制器,可以在容器中的三个UICollectionViews
之间切换。
HomeViewContainer是我的导航控制器的根
class HomeViewContainer: UIViewController {
let displayOptionSegmentedControl: UISegmentedControl = {
let segmentedControl = UISegmentedControl(items: ["Businesses","List","Map"])
segmentedControl.setupBasicControllerColors()
return segmentedControl
}()
let businessCollectionViewController = BusinessCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
let couponCollectionViewController = CouponCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
var currentSegmentIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.mainWhite()
setupHeaderView()
add(asChildViewController: businessCollectionViewController)
}
fileprivate func add(asChildViewController viewController: UIViewController) {
contentContainer.addChildViewController(viewController)
contentContainer.view.addSubview(viewController.view)
viewController.view.frame = view.bounds
viewController.didMove(toParentViewController: self)
}
fileprivate func remove(asChildViewController viewController: UIViewController) {
viewController.willMove(toParentViewController: nil)
viewController.view.removeFromSuperview()
viewController.removeFromParentViewController()
}
@objc func segmentedValueChanged(_ sender: UISegmentedControl) {
if currentSegmentIndex != sender.selectedSegmentIndex {
switch currentSegmentIndex {
case 0:
remove(asChildViewController: businessCollectionViewController)
case 1, 2:
remove(asChildViewController: couponCollectionViewController)
default:
break
}
//Add new view based on user chosen option.
switch sender.selectedSegmentIndex {
case 0:
currentSegmentIndex = sender.selectedSegmentIndex
add(asChildViewController: businessCollectionViewController)
case 1:
currentSegmentIndex = sender.selectedSegmentIndex
couponCollectionViewController.toggleMapHeaderOff()
add(asChildViewController: couponCollectionViewController)
case 2:
currentSegmentIndex = sender.selectedSegmentIndex
couponCollectionViewController.toggleMapheaderOn()
add(asChildViewController: couponCollectionViewController)
default:
break
}
}
}
}
视图可以正确显示,但是当我单击其中一个集合视图中的单元格时,就会出现问题。
例如,如果我在businessCollectionViewController中单击一个单元格,则尝试在导航控制器中显示另一个ViewController,但我不能。
BusinessCollectionViewController
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let bdvc = BusinessDetailViewController(collectionViewLayout: UICollectionViewFlowLayout())
//NOTHING HAPPENS HERE
self.navigationController?.present(bdvc, animated: false, completion: nil)
}
如何使 ContentContainer 的内容仍位于 HomeViewContainer 导航堆栈中?我怀疑它与我的 HomeViewContainer 类中的 add()方法有关,但是我不知道采用哪种合适的解决方案。
答案 0 :(得分:0)
找到了解决方案。我实际上需要将ContentContainer作为孩子添加到HomeViewContainer中。
在HomeViewContainer中
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.mainWhite()
//Added this line!
addChildVC(vc: contentContainer, toView: self.view)
setupHeaderView()
add(asChildViewController: businessCollectionViewController)
}
//And added this helper method
public func addChildVC(vc: UIViewController, toView containerView: UIView) {
addChildViewController(vc)
let frame = CGRect(x: 0, y: 0, width: containerView.frame.width, height: containerView.frame.height)
print("Container Frame = \(containerView.frame)")
vc.view.frame = frame
containerView.addSubview(vc.view)
vc.didMove(toParentViewController: self)
}