【发布时间】:2020-05-21 23:24:13
【问题描述】:
我创建了一个 UIView 类 (SegmentControl.swift) 并使用 xib 添加了 UISegmentControl。我有另一个视图控制器,我在其中引用这个 UIView 类来添加段控件。
我的问题是,由于我在另一个 UIView 类中有 UISegmentControl,我将如何更新索引并在 ViewController 中加载相应的 containerViews?
@IBAction func didChangeIndex(_ sender: UISegmentedControl) { } 仅当我在 ViewController 中有段控制时才能工作。
请提供关于当 UIControlSegment 在另一个类中时如何加载容器视图的建议。
这是我的代码:
SegmentControl.swift
class SegmentControl: UIView {
@IBOutlet weak var segmentView: UISegmentedControl!
// Loading nibs are there.. just that I didn't include here
func create(titles: [String]) {
items.enumerated().forEach { (index, item) in
segmentView.setTitle(item, forSegmentAt: index)
}
}
}
ViewController.swift
func showSegmentControl() {
let segmentedView = GenericSegmentedView.create(items: ["A", "B"])
stackView.addArrangedSubView(segmentedView)
}
两个容器视图 --> aInfoView 和 bInfoView 在 View Controller 中初始化。
由于它们在 UIView 类中,我将如何在切换段时加载它们。我在这里找不到任何答案。请帮忙!
谢谢!!
【问题讨论】:
-
这是一个非常好的解释:cocoacasts.com/…
标签: ios swift uisegmentedcontrol