当我尝试使用段来更改每个滚动中文本的名称时,我试图弄清楚我遇到的问题。 如果有人对它有好主意并且可以帮助我,我很高兴。
FirstViewController.swift
class FirstViewController: UIViewController ,ScrollUISegmentControllerDelegate{
@IBOutlet weak var Label1: UILabel!
@IBOutlet weak var Label2: UILabel!
@IBOutlet weak var Label3: UILabel!
@IBOutlet weak var segment: ScrollUISegmentController!
override func viewDidLoad() {
super.viewDidLoad()
segment.segmentDelegate = self
segment.tag = 1
segment.segmentItems = ["1","2","3","4","5","6","7","8","9","10"]
}
func selectItemAt(index: Int, onScrollUISegmentController scrollUISegmentController: ScrollUISegmentController) {
print("select Item At\(index) in scrollUISegmentController with tag \(scrollUISegmentController.tag) ")
}
@IBAction func SegeValueChange(_ sender: Any) {
if segment.selectedSegmentIndex == 1 {
Label1.text = " 100 "
Label2.text = " 40 "
Label3.text = " 400 "
}
if segment.selectedSegmentIndex == 2 {
Label1.text = " 100 "
Label2.text = " 50 "
Label3.text = " 400 "
}
if segment.selectedSegmentIndex == 3 {
Label1.text = " 100 "
Label2.text = " 60 "
Label3.text = " 600 "
}
}
}
ScrollUISegmentController.swift
//
// ScrollUISegmentController.swift
// ScrollUISegmentController
//
// Created by Ali Adam on 8/3/17.
// Copyright © 2017 Ali Adam. All rights reserved.
//
import UIKit
protocol ScrollUISegmentControllerDelegate: class {
func selectItemAt(index :Int, onScrollUISegmentController scrollUISegmentController:ScrollUISegmentController)
}
@IBDesignable
class ScrollUISegmentController: UIScrollView {
private var segmentedControl: UISegmentedControl = UISegmentedControl()
weak var segmentDelegate: ScrollUISegmentControllerDelegate?
@IBInspectable
public var segmentTintColor: UIColor = .black {
didSet {
self.segmentedControl.tintColor = self.segmentTintColor
}
}
@IBInspectable
public var itemWidth: CGFloat = 100 {
didSet {
}
}
public var segmentFont: UIFont = UIFont.systemFont(ofSize: 13) {
didSet {
self.segmentedControl.setTitleTextAttributes([NSAttributedStringKey.font: self.segmentFont],for: UIControlState())
}
}
public var itemsCount: Int = 3
public var segmentheight : CGFloat = 29.0
public var segmentItems: Array = ["1","2","3"] {
didSet {
self.itemsCount = segmentItems.count
self.createSegment()
}
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
createSegment()
}
override init(frame: CGRect) {
super.init(frame: frame)
createSegment()
}
init(frame: CGRect , andItems items:[String]) {
super.init(frame: frame)
self.segmentItems = items
self.itemsCount = segmentItems.count
self.createSegment()
}
func createSegment() {
self.segmentedControl.removeFromSuperview()
segmentheight = self.frame.height
var width = CGFloat(self.itemWidth * CGFloat(self.itemsCount))
if width < self.frame.width {
itemWidth = CGFloat(self.frame.width) / CGFloat(itemsCount)
width = CGFloat(self.itemWidth * CGFloat(self.itemsCount))
}
self.segmentedControl = UISegmentedControl(frame: CGRect(x: 0 , y: 0, width: width , height: segmentheight))
self.addSubview(self.segmentedControl)
self.backgroundColor = .clear
showsHorizontalScrollIndicator = false
showsVerticalScrollIndicator = false
NSLayoutConstraint(item: self.segmentedControl, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: self.segmentedControl, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0).isActive = true
let contentHeight = self.frame.height
self.contentSize = CGSize (width: width, height: contentHeight)
self.segmentedControl.setTitleTextAttributes([NSAttributedStringKey.font: self.segmentFont],for: UIControlState())
self.segmentedControl.tintColor = self.segmentTintColor
self.segmentedControl.selectedSegmentIndex = 0;
insertItems()
self.segmentedControl.addTarget(self, action: #selector(self.segmentChangeSelectedIndex(_:)), for: .valueChanged)
}
func insertItems(){
for item in segmentItems {
self.segmentedControl.insertSegment(withTitle: item, at: (segmentItems.index(of: item))!, animated: true)
}
}
@objc func segmentChangeSelectedIndex(_ sender: AnyObject) {
segmentDelegate?.selectItemAt(index: self.segmentedControl.selectedSegmentIndex, onScrollUISegmentController: self)
print("\(self.segmentedControl.selectedSegmentIndex)")
}
}
我有这个错误:
'ScrollUISegmentController'类型的值没有成员'selectedSegmentIndex'
答案 0 :(得分:3)
我认为您正在使用第三方ScrollUISegmentController。您必须实现委托方法才能获得所选索引
func selectItemAt(index: Int, onScrollUISegmentController scrollUISegmentController: ScrollUISegmentController) {
if index== 1 {
Label1.text = " 100 "
Label2.text = " 40 "
Label3.text = " 400 "
}
if index == 2 {
Label1.text = " 100 "
Label2.text = " 50 "
Label3.text = " 400 "
}
if index == 3 {
Label1.text = " 100 "
Label2.text = " 60 "
Label3.text = " 600 "
}
}
答案 1 :(得分:0)
请找到您的ScrollUISegmentController
,其中UISegmentedControl
的私有变量名为segmentedControl
,请从那里删除private
。
现在,您可以使用selectedSegmentIndex
访问segment.segmentedControl.selectedSegmentIndex
。
希望它有效。
<强>更新强>
或者您可以像ScrollUISegmentController
这样创建此属性,
var selectedIndex: Int {
return self.segmentedControl.selectedSegmentIndex
}
然后您可以像segment.selectedIndex
它很漂亮。看一看。