Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Hamza Ansari
1,014 PointsHow do I connect a button to this function?
I have this function written in Objective-C:
- (NSString *) addVote:(NSString *) countOption1 {
NSString *result = [countOption1
stringByAppendingString:countOption1];
return result;
}
I'm trying to connect it to a button on my ViewController. I'm using the Parse.com SDK template but I'm not sure how to make my button execute this function, which is supposed to add a vote to a poll option (the button is one of two options in a poll) and then return the poll results.
When I hold control and click and drag the button to the assistant editor, does it need to be in ViewController.h or ViewController.m? Also, once my IBAction function appears in the assistant editor, how do I add the above function to the IBAction so that my button executes that function?
4 Answers
Andres Oliva
7,810 PointsIt depends on where you have declared and implemented addVote.
It should be on your model, nevertheless, I assume you've declared it in the viewController. If this is the case then
[self addVote:(argument)];
should do the trick.
(argument) is the NSString *countOption thing that you provide to the function in order to work.
Andres Oliva
7,810 PointsEasy. To connect a button to your code you just need to ctrl drag the button into your code, then it will create an IBAction. You need to drag it to your .m file in the @implementation block. Do not drag it into the @interface block, otherwise you will create an IBOutlet, which is a @property, not a function.
Once you have the IBAction created, you just need to call addVote: in the button's action.
Hope it is clear enough!
Hamza Ansari
1,014 PointsThanks for the reply, but how do I call addVote in the button's action?
Andres Oliva
7,810 PointsOh no, you are doing it wrong. Having addVote as a separate function was ok, you just needed to call it inside the action, like this:
- (NSString *) addVote:(NSString *) countOption1 {
NSString *result = [countOption1
stringByAppendingString:countOption1];
return result;
}
- (IBAction)addOneVote {
[self addVote: string]
}
Also I don't really understand what countOption1 is, and why is it a label?
Hamza Ansari
1,014 PointscountOption1 is supposed to be one of the choices for the poll that can be selected, I just haven't given it a name yet. There are supposed to be 1-3 other choices within the poll.
Also, when I used that code I got an error on the last line that said, "Use of undeclared identifier 'string', was I supposed to write something else in place of the word "string"? Thanks again for all the responses, it is so greatly appreciated!
Andres Oliva
7,810 PointsMm ok.
First of all, you shouldn't declare an IBOutlet in your .h file. You want to keep this connections private to the viewController.
Also I would recommend not to use the same name for the UILabel and the function parameters. It is ambiguous.
Honestly, I can't visualize what you are doing, but the way I see it, this is what you need:
- (NSString *) addVote:(NSString *) countOption1 {
NSString *result = [countOption1
stringByAppendingString:countOption1];
return result;
}
- (IBAction)addOneVote {
[self addVote: self.countOption.text];
}
Andres Oliva
7,810 Pointsself.countOption.text is the text property of your label.
Hamza Ansari
1,014 PointsThat fixed the error, thank you very much! Really appreciate the help!
Hamza Ansari
1,014 PointsHamza Ansari
1,014 PointsThanks again, I really appreciate it. I added that to my IBAction and now have it written like below in my .m file, but I'm getting an error on the second line that states, "Expected identifier". It doesn't make sense because all of my brackets are matched up.
Also I added @synthesize countOption1 right below it in the .m file and in the .h file, I added:
@property (weak, nonatomic) IBOutlet UILabel *countOption1;