简体   繁体   English

快速从另一个类更改变量的值

[英]changing the value of a variable from another class in swift

I have created a custom UISegment Control 我创建了一个自定义UISegment控件

@IBDesignable class CardsSegmentedControl: UIControl {

private var labels = [UILabel]()
var thumbView = UIView()


var items: [String] = ["Saved Cards", "Add Card"] {
    didSet {
        setupLabels()
    }
}

var selectedIndex : Int = 0 {
    didSet {
        displayNewSelectedIndex()
    }
}
....
}

Now I wish to change the value of the variable selectedIndex in the viewController where I am adding this custom segment control in. 现在,我想在其中添加此自定义细分控件的viewController中更改变量selectedIndex的值。

I guess it is a problem of how to access/change variables from another class. 我想这是一个如何从另一个类访问/更改变量的问题。

I tried to create a class func which would set the value of the selectedIndex but I cannot get it to access the selectedIndex variable either. 我试图创建一个class func ,该class func将设置selectedIndex的值,但是我也无法访问它来访问selectedIndex变量。

Still pretty new to Swift so please bear with me. 对Swift来说还是很新的,所以请忍受我。

// Inside your ViewController class, create a new instance of your custom class
var cardSegmentedControl = CardSegmentedControl()

// here, change its property value
cardSegmentedControl.selectedIndex = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速将从@转义接收到的值分配给另一个类中的实例变量 - Assign value received from @escaping to instance variable in another class in swift 从Swift中的另一个类引用@IBAction函数中的变量值 - Reference a variable value inside an @IBAction function from another class in Swift 变量从另一个swift文件更改其值后返回nil值 - Variable returns nil value after changing its value from another swift file 如何在Swift中的另一个类中设置变量值 - How to set a variable value in another class in Swift 无法将变量值从 firebase 防火墙传递到另一个 class SWIFT - Can't pass variable value from firebase firestore to another class SWIFT 快速访问另一个类的类类型变量 - access a class type variable from another class in swift 在另一个类iOS中更改变量 - Changing Variable in another class iOS 无法将变量从类中的函数传递给Swift中的另一个函数 - cannot pass a variable from a function in a class to another function in Swift 如何快速从另一个类访问变量? - How to get access to variable from another class in swift? 是否可以根据 class 中的另一个变量初始化变量,该变量继承自 Swift 中的 UIViewController? - Is it possible to initialize variable depending on another in class inheriting from UIViewController in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM