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.

3
  • do you have init func? Commented Aug 24, 2015 at 9:12
  • no I dont.. Should I set the value of selectedIndex using the init function and then use it to change the value elsewhere as well? Commented Aug 24, 2015 at 9:13
  • selectedIndex is instance variable, so won't be accessed from class functions Commented Aug 24, 2015 at 9:14

1 Answer 1

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

// here, change its property value
cardSegmentedControl.selectedIndex = 1
Sign up to request clarification or add additional context in comments.

1 Comment

Take a look at this Stackoverflow thread for understanding willSet and didSet actions: stackoverflow.com/questions/24006234/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.