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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 07-15-2010, 02:33 PM   #1 (permalink)
Wes
Registered Member
 
Join Date: Jul 2010
Posts: 7
Wes is on a distinguished road
Question UISegmentedControl - basic math, division, changing a value of an integer.

Hello kind SDK Masters,

I'm writing a small application which takes a number from a textfield, divides it by 18 or 27, and prints the result in a label. I would like to use segmented control to change that second value to 18 when selectedSegmentIndex == 0, and 27 when selectedSegmentIndex == 1. Could anyone tell me how to do that? Thank you in advance.

Here's the code I'd like to transform:

- (IBAction)calc {
float a = ([textField.text floatValue]);
float b = a/18;
float c = a/27;
label.text = [[NSString alloc] initWithFormat:@"%f", b];
label.text = [[NSString alloc] initWithFormat:@"%f", c];
}
Wes is offline   Reply With Quote
Old 07-15-2010, 02:41 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 31
George0107 is on a distinguished road
Default

Look up UISegmentedControl, actually here:

Herel

There's a property that returns the selected segment..

So you would check which segment is selected, (either 18, or 27)

then divide accordingly.
George0107 is offline   Reply With Quote
Old 07-15-2010, 02:46 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Benicia, CA
Age: 50
Posts: 152
That Don Guy is on a distinguished road
Default

Unless I'm missing something, how about:
Code:
- (IBAction)calc {
float a = ([textField.text floatValue]);
float b = a / (mySegmentedControl.segmentedSegmentIndex * 9.0 + 18.0);
label.text = [[NSString alloc] initWithFormat:@"%f", b];
}
where mySegmentedControl is the name of your UISegmentedControl variable?

(If you need information on how to add the segmented control and attach it to the code, it should be a fairly simple case of adding to the .h file:

IBOutlet UISegmentedControl *mySegmentedControl;

then add the segmented control in Interface Builder and attach a referencing outlet from the control to mySegmentedControl. Am I missing anything, given that all he really needs is the selected segment number?)

-- Don
That Don Guy is offline   Reply With Quote
Old 07-15-2010, 03:50 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 31
George0107 is on a distinguished road
Default

Why * 9 and + 18??

But what you have is pretty much what he will need, except the part that I mentioned above ^.

I would of done a switch:

Code:
float a = [[textField text] floatValue];
int index = [UISegmentedControl selectedSegmentIndex];
switch(index) {
case 0: //18
[label setText:[NSString stringWithFormat:@"%@", (a / 18)]; //is label mutable? 
break;
case 1: //27
[label setText:[NSString stringWithFormat:@"%@", (a / 27)];
break;
}
George0107 is offline   Reply With Quote
Old 07-15-2010, 04:14 PM   #5 (permalink)
Wes
Registered Member
 
Join Date: Jul 2010
Posts: 7
Wes is on a distinguished road
Default

Quote:
Originally Posted by George0107 View Post
Why * 9 and + 18??

But what you have is pretty much what he will need, except the part that I mentioned above ^.

I would of done a switch:

Code:
float a = [[textField text] floatValue];
int index = [UISegmentedControl selectedSegmentIndex];
switch(index) {
case 0: //18
[label setText:[NSString stringWithFormat:@"%@", (a / 18)]; //is label mutable? 
break;
case 1: //27
[label setText:[NSString stringWithFormat:@"%@", (a / 27)];
break;
}
All hail, thank you so much George, that will work.

I was trying, badly of course, to pull it off like this:

Code:
// .h file

@interface Untitled : UIViewController {
	IBOutlet UISegmentedControl *choose;
	IBOutlet UITextField *textField;
	IBOutlet UILabel *label;
}

- (IBAction)calc;
- (IBAction)change;

// .m

-(IBAction)calc {
   float a = ([textField.text floatValue]);
   float b = a/y;
   label.text = [[NSString alloc] initWithFormat:@"%f", b];

-(IBAction)change { 
   switch (choose.selectedSegmentIndex) {
      case 0:
         y = 18;
	 break;
      case 1:
          y = 27;
          break;
      default:
	  break;
     }
}
That's not gone well, has it...

Last edited by Wes; 07-15-2010 at 04:23 PM.
Wes is offline   Reply With Quote
Old 07-15-2010, 04:45 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Benicia, CA
Age: 50
Posts: 152
That Don Guy is on a distinguished road
Default

Quote:
Originally Posted by George0107 View Post
Why * 9 and + 18??
If selectedSegmentIndex = 0, the divisor is 0 * 9 + 18 = 18.
If selectedSegmentIndex = 1, the divisor is 1 * 9 + 18 = 27.

-- Don
That Don Guy is offline   Reply With Quote
Old 07-16-2010, 01:50 AM   #7 (permalink)
Wes
Registered Member
 
Join Date: Jul 2010
Posts: 7
Wes is on a distinguished road
Default

Quote:
Originally Posted by George0107 View Post
Why * 9 and + 18??

But what you have is pretty much what he will need, except the part that I mentioned above ^.

I would of done a switch:

Code:
float a = [[textField text] floatValue];
int index = [UISegmentedControl selectedSegmentIndex];
switch(index) {
case 0: //18
[label setText:[NSString stringWithFormat:@"%@", (a / 18)];
break;
case 1: //27
[label setText:[NSString stringWithFormat:@"%@", (a / 27)];
break;
}
I'm getting the following warnings:

Code:
int index = [UISegmentedControl selectedSegmentIndex]; 
/* 
1. 'UISegmentedControl' may not respond to '+selectedSegmentIndex'
(Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.)
2. Initialization makes integer from pointer without a cast 
*/
And when I try to calculate, the application gets terminated for some reason.
Wes is offline   Reply With Quote
Old 07-16-2010, 01:58 AM   #8 (permalink)
Wes
Registered Member
 
Join Date: Jul 2010
Posts: 7
Wes is on a distinguished road
Default

Quote:
Originally Posted by That Don Guy View Post
If selectedSegmentIndex = 0, the divisor is 0 * 9 + 18 = 18.
If selectedSegmentIndex = 1, the divisor is 1 * 9 + 18 = 27.

-- Don
Problem solved, That Don God Thanks.
Wes is offline   Reply With Quote
Old 07-19-2010, 02:45 PM   #9 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Benicia, CA
Age: 50
Posts: 152
That Don Guy is on a distinguished road
Default

Quote:
Originally Posted by George0107 View Post
I would of done a switch:

Code:
float a = [[textField text] floatValue];
int index = [UISegmentedControl selectedSegmentIndex];
switch(index) {
case 0: //18
[label setText:[NSString stringWithFormat:@"%@", (a / 18)]; //is label mutable? 
break;
case 1: //27
[label setText:[NSString stringWithFormat:@"%@", (a / 27)];
break;
}
Quote:
Originally Posted by Wes View Post
I'm getting the following warnings:

Code:
int index = [UISegmentedControl selectedSegmentIndex]; 
/* 
1. 'UISegmentedControl' may not respond to '+selectedSegmentIndex'
(Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.)
2. Initialization makes integer from pointer without a cast 
*/
And when I try to calculate, the application gets terminated for some reason.
There are two problems in the code.

First, in:
int index = [UISegmentedControl selectedSegmentIndex];
change "UISegmentedControl" to the actual name of your segmented control.

Second, in the two stringWithFormat calls, the format string includes %@, which is for a string, and it is expecting a string (actually, a pointer to an NSString) as a parameter, but the code has an int as the parameter. To include an int in a format string, use %i (or %qi for a "long long"), not %@.

-- Don
That Don Guy is offline   Reply With Quote
Reply

Bookmarks

Tags
math, uisegmentedcontrol

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: 332
14 members and 318 guests
akphyo, alexP, appservice, cgokey, EXOPTENDAELAX, flamingliquid, guusleijsten, mariano_donati, ohmniac, Paul Slocum, PavelSea, SLIC, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,653
Threads: 94,115
Posts: 402,888
Top Poster: BrianSlick (7,990)
Welcome to our newest member, ohmniac
Powered by vBadvanced CMPS v3.1.0

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