我想实现一个分段控制器,在交换机上将UICollectionViewController加载到视图中。另外,我也想知道如何让应用程序在应用程序ViewDidLoad方法上显示这个UICollectionViewController。见下面的代码。提前谢谢!
var segmentedController: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
let items = ["Label A", "Label B"]
segmentedController = UISegmentedControl(items: items)
navigationItem.titleView = segmentedController
segmentedController.selectedSegmentIndex = 0
// function to handle Value Changed events
segmentedController.addTarget(self, action: #selector(HomeController.segmentedValueChanged(_:)), for: .valueChanged)
}
// reference to collectionViewController
var travelersFeedCollectionVC: UICollectionViewController!
func segmentedValueChanged(_ sender:UISegmentedControl!)
{
print("Selected Segment Index is : \(sender.selectedSegmentIndex)")
switch segmentedController.selectedSegmentIndex {
case 0:
self.view.isHidden = true
// load travelersFeedCollectionVC here.............
case 1:
self.view.isHidden = false
default: // Do nothing
break
}
}
答案 0 :(得分:1)
您可以使用容器视图技术。基本上您需要定义主容器视图。在容器视图内部,您可以轻松地显示所需的任何视图控制器。请参阅本教程以了解有关此技术的更多信息。链接 - https://guides.codepath.com/ios/Container-View-Controllers-Quickstart