Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-19-2010, 07:58 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 22
Default simple UISegmentControl question

Hi,

I have a problem using UISegmentControl.

I want to calculate a formula, but the formula is different for men / women.


Quote:
- (IBAction) calcWomen: (id) sender
{

int a = [weightTF.text intValue];
int b = [lenghtTF.text intValue];
int c = [ageTF.text intValue];
int cal = 655 + (9.6 * a) + (1.8 * b) - (4.7 * c);

totalTF.text = [NSString stringWithFormat:@"%i", cal];


}


Quote:
- (IBAction) calcMen: (id) sender
{

int a = [weightTF.text intValue];
int b = [lenghtTF.text intValue];
int c = [ageTF.text intValue];
int cal = 655 + (9.6 * a) + (1.8 * b) - (4.7 * c);

totalTF.text = [NSString stringWithFormat:@"%i", cal];

}
How can I make one calculate button by using a UISegmentControl where you can choose between male of female?

I tried this, but then I need to declare cal again...

Quote:
- (IBAction) changeSegment{
if (genSegment.selectedSegmentIndex == 0) {
int a = [weightTF.text intValue];
int b = [lenghtTF.text intValue];
int c = [ageTF.text intValue];
int cal = 66 + (13.7 * a) + (5 * b) - (6.8 * c);

}
if (genSegment.selectedSegmentIndex == 1) {
int a = [weightTF.text intValue];
int b = [lenghtTF.text intValue];
int c = [ageTF.text intValue];
int cal = 655 + (9.6 * a) + (1.8 * b) - (4.7 * c);
}

}


- (void) calc: (id) sender
{
totalTF.text = [NSString stringWithFormat:@"%i", cal];
}

Last edited by yannickd60; 03-19-2010 at 08:01 AM.
yannickd60 is offline   Reply With Quote
Old 03-21-2010, 12:50 PM   #2 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 22
Default

Nobody?

It can't be that difficult?
yannickd60 is offline   Reply With Quote
Old 03-21-2010, 01:28 PM   #3 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,578
Default

It's not clear what the problem is. But if I were you I would do it this way:

Code:
- (IBAction) changeSegment{
if (genSegment.selectedSegmentIndex == 0) {
    [self calcMen];
} else {
    [self calcWomen];
}
Just to keep it simpler.
JasonR is online now   Reply With Quote
Old 03-21-2010, 03:51 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 22
Default

Here is a screen capture of my view to make it more clear:


How do I have to connect the IBActions in IB then?

'change value' or 'touch up inside' for the UISegment control?
What IBactions must be connected to the calculate-button?

Thanks for the reply
yannickd60 is offline   Reply With Quote
Old 03-21-2010, 04:22 PM   #5 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,578
Default

I would probably use it for "valueChanged" in the segmented Control, and "touchUp inside" for the button.
JasonR is online now   Reply With Quote
Old 03-21-2010, 04:26 PM   #6 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 22
Default

and which IBAction do I have to connect to the calculate-button??

Because you use both calc-actions in the UISegmentControl function?
When I use value changed the whole app crashes in simulator...
yannickd60 is offline   Reply With Quote
Old 03-21-2010, 04:39 PM   #7 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,578
Default

calcMen and calcWomen really shouldn't be IBActions because there's no place I can see you want to do only one or the other. You might rename "changeSegment" to something like "doCalculate" because that's what it's for.

And I can't see anything you posted that should crash, so you'll need to specify what line is crashing and any messages you are getting.
JasonR is online now   Reply With Quote
Old 03-21-2010, 07:16 PM   #8 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 22
Default

Hi,

It works fine now by using only the UISegmentControl, but I still can't make use of the calculate-button.

The total value only changes when I switch from male to female or female to male.
I had in mind that it only calculates the total value depending on the UISegmenControl just by clicking the calculate button.

Thanks
yannickd60 is offline   Reply With Quote
Old 03-21-2010, 07:46 PM   #9 (permalink)
iPhone SDK learner
 
Join Date: Feb 2010
Location: Illinois, USA
Posts: 417
Default

Quote:
Originally Posted by yannickd60 View Post
Hi,

It works fine now by using only the UISegmentControl, but I still can't make use of the calculate-button.

The total value only changes when I switch from male to female or female to male.
I had in mind that it only calculates the total value depending on the UISegmenControl just by clicking the calculate button.

Thanks
You don't even need an IBAction for the UISegmentControl, just an IBOutlet. Than in the buttons touchUpIniside action put the code
Code:
-(IBAction)yourButtonPressed {
      if (yourSegementControl.selectedSegmentIndex == 0) {
           [self calcMen];
    } else if (yourSegmentedControl.selectedSegmentIndex == 1) {
             [self calcWomen];
       }
}
Not tested, might be a few typos
Batman is offline   Reply With Quote
Old 03-21-2010, 11:46 PM   #10 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,578
Default

It looks like you would just use the same IBAction for the button. I don't see any reason why not.
JasonR is online now   Reply With Quote
Old 03-22-2010, 08:44 AM   #11 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 22
Default

Thank you for the replies!

It works perfect now...

The reason why it crashes was because of the "(id) sender" after the IBAction in the header file
yannickd60 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 240
19 members and 221 guests
ADY, AragornSG, BrianSlick, Dani77, Dattee, dre, glenn_sayers, HemiMG, JasonR, karlam963, nobre84, Oral B, prchn4christ, Raggou, Rudy, spiderguy84, themathminister, viniciusdamone, vvenkatachallam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,884
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, karlam963
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:20 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0