2

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.

genaks
  • 757
  • 2
  • 10
  • 24

1 Answers1

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

// here, change its property value
cardSegmentedControl.selectedIndex = 1
Vito Royeca
  • 657
  • 10
  • 20
  • Take a look at this Stackoverflow thread for understanding willSet and didSet actions: http://stackoverflow.com/questions/24006234/what-is-the-purpose-of-willset-and-didset-in-swift – Vito Royeca Aug 24 '15 at 09:37