0

I have this UISwitch that I connected from my storyboard to my controller.

@property (weak, nonatomic) IBOutlet UISwitch *wantHelp;

What I am trying to do is to configure it so that the app can know when the state of the uiswitch has changed.

I looked at examples online and they show something similar to this:

-(IBAction)helpToggle:(id)sender
{
    if (wantHelp.on) 
    {
        NSLog(@"yes");
    }
    else 
    {
        NSLog(@"No");
    }
}

but they seem to refer to different ids like the wantHelp or the helpToggle and many of the examples use this kind of a heading -(IBAction)helpToggle:(id)sender but I am confused what the "sender" is configured from and what it should be in my case.

Thanks for your help in helping me understand what to do.

GeekedOut
  • 16,905
  • 37
  • 107
  • 185
  • I am not certain how to configure the helpToggle. I added this to my .h file - (IBAction)wantHelp:(id)helpToggle; @property (weak, nonatomic) IBOutlet UISwitch *wantHelp; but I think its pretty wrong. – GeekedOut Jul 12 '12 at 21:29

2 Answers2

1

Every time you flip the switch (wantHelp), the method helpToggle is called. When helpToggle is called, the if statements check if the conditions are true, in this case (wantHelp.on). If wantHelp is on, then the code within that if statement is called, otherwise the else statement is called.

As for the sender tag, it's what allows for communication for the method back to the switch I believe.

Usual: Object > Method

Sender: Object > Method > Back to Object

I am Root.
  • 131
  • 7
  • @I am Root. Thanks, but I am not sure what to do to make sure that block of code gets called when the switch is flipped. Any idea what I have wrong in my code above? Thanks! – GeekedOut Jul 12 '12 at 21:27
1

sender is the object that's sending the message. If you have your UISwitch hooked up to send a helpToggle: action to an object that implements it, sender will be a pointer to the switch.

If you want to check it out for yourself, add a line like this to your code:

`NSLog(@"sender is: %@", sender);`
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • thank you, what I really wanted was something like a listener that would get triggered when the switch value changes. But when I try it, that block of code never gets triggered. Would you know why? Or how to set it up so it gets triggered? – GeekedOut Jul 12 '12 at 21:25
  • That's exactly what it should do. I even tried your code in a sample project -- works fine. If your action isn't being triggered when you flip the switch, you probably haven't connected the switch to a target and action. Assuming that your `-helpToggle:` method is in a view controller, drop a switch in that controller's view's .xib (or storyboard). Then control-drag from the switch to the view controller, and select the `-helpToggle:` action. Note: this is separate from connecting the switch to one of your view controller's outlets. – Caleb Jul 12 '12 at 21:35
  • How do I hook up my UISwitch to send a helpToggle: - thats what I am not sure about. – GeekedOut Jul 12 '12 at 21:36
  • I seem to have connected them, and now when I toggle the switch I get this error: unrecognized selector sent to instance 0x6a89c40 – GeekedOut Jul 12 '12 at 21:44