When I created a UISwitch element in my storyboard, and attached it to the .h file, there was this line generated in the controller:
@property (weak, nonatomic) IBOutlet UISwitch *wantHelp;
And I was trying to figure out how to make the system notice when the value was changed, so I tried to manually add something like this below the original statement:
- (IBAction)wantHelp:(id)helpToggle;
and in the controller, I have something like this:
-(IBAction)helpToggle:(id)sender
{
NSLog(@"sender is: %@", sender);
if (wantHelp.on)
{
NSLog(@"yes");
}
else
{
NSLog(@"No");
}
}
What I wanted to do is connect the helpToggle IBAction with the particular UISwitch element, but when I run it, I get an error: unrecognized selector sent to instance which as I understand is means that I called an operation on an incorrect object.
Could someone help me understand what I am doing wrong here?
Thanks!!
This is how my .m file looks like:
@interface PlanBusinessController ()
@end
@implementation PlanBusinessController
@synthesize businessDescription;
@synthesize personName;
@synthesize personEmail;
@synthesize privacy;
@synthesize wantHelp;
-(IBAction)helpToggle:(id)sender
{
NSLog(@"sender is: %@", sender);
if (wantHelp.on)
{
NSLog(@"yes");
}
else
{
NSLog(@"No");
}
}
So you can have as many
IBActionas you want or one it doesnt matter. What matters most is declaring it in your .h file, going into interface builder and linking the desired action to thatIBAction.so you have your
IBActionthen go to interface builder and click on your File’s Owner object(kinda top left). then click on the Connections Inspector (top right)
and drag from the dot where it says WHATEVERNAME to your object (
UISwitch wantHelp)when you do that it will ask you what action you want to receive
take in mind the reason for
(id)senderis that you are suppose to cast your objectso that you have a pointer to the switch object
this is because you could have this same IBAction method handle many different types of objects
although this is all good stuff the actual error came from multiple selectors being called. Once we got into the connection inspector we were able to see the 2 different methods being called by it. One was the valid IBAction and the other was a previously declared method wantHelp