Rafa
Rafa

Reputation: 13

How to use UISwitch

I am new in Xcode, trying to build an app, and I am studying a lot; it seems not enough. Anyway, I need a step by step of 2 things:

  1. my 1st Vc has 10 switch buttons and I need to limit to only 4 can be "On";

  2. when my switch button is on, it changes a label in the 2nd Vc.

I am really struggling with the segues codes. Any light will be highly appreciated.

Upvotes: 0

Views: 54

Answers (1)

Ryan
Ryan

Reputation: 815

Step 1

i. You need to have a data model of an array of Bool to store the state of the UISwitch

ii. When the value of the UISwitch changes, you have to update the value of the corresponding index of the Bool array

iii. To limit the number of On, you may disable all Off switch if the limit exceeds so that the user can only switch On to Off but not adding another On switch. Or you may listen to the valueChanged event, if the 5th switch is On, immediately set it back to Off

Step 2

You need to learn how to pass data/objects between ViewControllers.

i. the most simple one would be having a Singleton object that can be accessed anywhere.

ii. but it may be better to have a property in the 2nd VC, just pass the data model before 2nd VC is pushed

Upvotes: 0

Related Questions