简体   繁体   中英

changing the value of a variable from another class in swift

I have created a custom UISegment Control

@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.

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.

Still pretty new to Swift so please bear with me.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM