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];
}
- (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?)
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.
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
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 %@.