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 trialPETRA LUTZ
2,922 Pointsnsurlconnection http post request to post button value
I want to post the value of a UIButton using its tag, I used to post UItextField using this code :
if([[self.fieldOne text] isEqualToString:@""] || [[self.fieldTwo text] isEqualToString:@""] ) { [self alertStatus:@"Please fill all the form" :@"Sign in Failed!" :0]; } else { NSString *post =[[NSString alloc] initWithFormat:@"name=%@&fieldOne=%@&fieldTwo=%@",[self.fieldOne text], [self.fieldTwo text]]; NSURL *url=[NSURL URLWithString:@"http://example.com/page.php"];
Any tips on how I can do the same for UIButton ? As I have 3 radio buttons on the same tab.
11 Answers
Holger Liesegang
50,595 PointsHi Petra,
it's very easy to identify a UIButton by its tag (nameOfButton.tag) but what do you mean by "radio buttons" as you wrote you're using UIButtons for this purpose. Wouldn't possibly a segmented control or switch make more sense for your purpose but maybe I'm just not getting the gist of your problem :-) It would be very helpful If you could go into more details here...
Kind Regards Holger
Holger Liesegang
50,595 PointsYou can press a UIButton (Touch Up Inside for example) but what do you mean by "selects one of these three buttons" - I mean how do you select a UIButton? Do you programmatically change the UIButton instance if it has been pressed and then programmatically change the other two UIButton instances to an "unpressed state" of art? Why don't you like to use a segmented control or switch as each of these would be IMHO more adequate to the task at hand.
...then putting the post request together isn't hard but I really need to know how you utilize the UIButtons in this context before :)
PETRA LUTZ
2,922 PointsHere is how I use it :
- (IBAction)btnOneClicked:(UIButton *)sender { btn1.selected = YES; btn2.selected = NO; btn3.selected = NO; } - (IBAction)btnTwoClicked:(UIButton *)sender { btn1.selected = NO; btn2.selected = YES; btn3.selected = NO; } - (IBAction)btnThreeClicked:(UIButton *)sender { btn1.selected = NO; btn2.selected = NO; btn3.selected = YES; }
Holger Liesegang
50,595 Pointsyou could use a property like
@property (weak, nonatomic) UIButton *selectedButton;
and then
(IBAction)btnOneClicked:(UIButton *)sender {
self.selectedButton = sender;
}
(IBAction)btnTwoClicked:(UIButton *)sender {
self.selectedButton = sender;
}
(IBAction)btnThreeClicked:(UIButton *)sender {
self.selectedButton = sender;
}
and in your URL post generating method kind of
NSString *buttonName = @"";
// The button with tag 10 is named "Buttontext10" for example and so on...
if (self.selectedButton.tag == 10) {
buttonName = @"Buttontext10";
} else if (self.selectedButton.tag == 20) {
buttonName = @"Buttontext20";
} else if (self.selectedButton.tag == 30) {
buttonName = @"Buttontext30";
}
NSString *post =[[NSString alloc] initWithFormat:@"name=%@&fieldOne=%@&fieldTwo=%@&buttonText=%@",[self.fieldOne text], [self.fieldTwo text], buttonName];
for example :)
Holger Liesegang
50,595 Pointsplease refresh, I corrected some code :-)
PETRA LUTZ
2,922 PointsWell, for this it crashes on this line:
self.selectedButton = sender;
Holger Liesegang
50,595 Pointsand in your URL post generating method kind of
NSString *post =[[NSString alloc] initWithFormat:@"name=%@&fieldOne=%@&fieldTwo=%@&buttonText=%@",[self.fieldOne text], [self.fieldTwo text], self.selectedButton.currentTitle];
would be a smoother solution than the one with the tags of course :-)
Holger Liesegang
50,595 PointsI just tested a UIButton with an IBAction as above and self.selectedButton = sender;
runs fully functional so I don't know where the problem might be in your case as you've go a lot more code running :)
PETRA LUTZ
2,922 PointsCan you please give me, the full code working with you ? knowing that when I select a button, the other ones got unselected (like in the sample I provided)
Holger Liesegang
50,595 PointsIn my short test it's just a simple button on a view
and property
@property (weak, nonatomic) UIButton *buttonPressed;
and the IBAction
- (IBAction)testButtonPressed:(UIButton *)sender {
self.buttonPressed = sender;
}
to verify your crash...
You've got the last pressed (aka "current selected") UIButton instance in the selectedButton property. You don't have to "unselect" another button or, as I already asked you, do you programmatically modify the graphical appearance of the instances of these UIButtons?
PETRA LUTZ
2,922 PointsYes, I do. Because I have like three radio buttons in one tab. The user will have to pick one choice (one button) then press post.
PETRA LUTZ
2,922 PointsThat's why I am using this :
- (IBAction)btnOneClicked:(UIButton *)sender { btn1.selected = YES; btn2.selected = NO; btn3.selected = NO; } - (IBAction)btnTwoClicked:(UIButton *)sender { btn1.selected = NO; btn2.selected = YES; btn3.selected = NO; } - (IBAction)btnThreeClicked:(UIButton *)sender { btn1.selected = NO; btn2.selected = NO; btn3.selected = YES; }
PETRA LUTZ
2,922 PointsCan you please give me an appropriate answer based on my code and what I need ? Thanks in advance !
Holger Liesegang
50,595 Pointsok, then you just add the
btn1.selected = YES/NO;
btn2.selected = YES/NO;
btn3.selected = YES/NO;
to the three (IBAction)btnXClicked: methods again to graphically modify these buttons accordingly.
Holger Liesegang
50,595 PointsMaybe it's to complex a topic to handle in a forum and without any further insight into your code (e.g. Github?) I'm afraid I couldn't be of more help for you, Petra - I'm sorry.
Kind Regards Holger
PETRA LUTZ
2,922 PointsIs there anything you recommend me to do ?
PETRA LUTZ
2,922 PointsPETRA LUTZ
2,922 PointsThanks a lot for your answer, what I have is 3 buttons in a single tab. I want for example when a user selects one of these three buttons and push the "post" button, a post request will be sent to the server in order to insert a row in DB. This is exactly what I want to do.