Question

Forgive me if this is a simple question, but is a UISwitch a boolean variable? If it is not, how can I make it BOOL?

Please allow me to explain why I'm having trouble.

I declared my variable in my FlipsideViewController.

UISwitch* mySwitch;     //switch used to turn label2 on/off

also creating an accessor

@property (nonatomic, retain) IBOutlet UISwitch *mySwitch;

EDIT: and of course I synthesize it.

Then in Interface Builder I connected mySwitch to a Switch I created on that view.

NOW, I have a method that I'm using which is this in the MainViewController:

- (void)updateLabels:(NSString *)text :(BOOL)isOn {

    [nameLabel setText:text]; 
    if (isOn==YES)
    [onLabel setText:(@"ON")];
      else
       [onLabel setText:(@"OFF")]; }

The problem I'm having is that whenever I run the program, the label always displays "OFF". Do you have any clue why? I'm really helpless.

I would really appreciate any help on this, and again I apologize for being such a newbie. :)

EDIT: This is how I'm calling the method...

 [self.delegate updateLabels: myTextField.text : mySwitch.state];

So, I'm passing in the state of the mySwitch UISwitch.

Was it helpful?

Solution

You either need to connect the UISwitch to an IBAction method that updates isOn, or read the state of the switch in your test.

Here's the latter: UPDATE Changed the code after you clarified the relationships between the view controllers.

- (void)updateLabels:(NSString *)text isOn:(BOOL)isOn {
    [nameLabel setText:text]; 
    if (isOn == YES) {
       [onLabel setText:(@"ON")];
    } else {
       [onLabel setText:(@"OFF")]; 
    }
}

The call to updateLabels, which I'm hoping exists in FlipSideViewController, should look something like this: (I obviously don't know your variables names.)

[self.delegate updateLabels:self.myTextField.text isOn:self.mySwitch.on];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top