setting the objectForKey with a string - iphone

I am building an app where I need to change some helpful information based on what stage the user is at:
I used strings labeled Stage1 ... Stage7 in the dictionary and I want to display the helpful info from each Stage wen the user moves the slider.
NSDictionary *foodInfo = [foodArray objectAtIndex:row];
NSInteger numberLookup = lroundf([stageSlider value]);
NSString* stageText = #"Stage";
stageText = [stageText stringByAppendingFormat:#"%i", numberLookup];
NSNumber *helps = [foodInfo objectForKey:#"Stage1"]; // Need stageText string instead "Stage1" shown?
notesLabel.text = helps;
Currently I display the "Stage1" text no matter where the slider is positioned and I have verified that "stageText" is incrementing/decrementing just fine.
How do I put the stageText string in instead of the text string shown?
Thanks for the help.
padapa

Like that ? NSNumber *helps = [foodInfo objectForKey:[NSString stringWithFormat:#"stage%d",numberLookup]];

NSNumber *helps = [foodInfo objectForKey:stageText];

Related

Stepper value reset after loaded from coredate

I am building a UITabledetail view, which contains a stepper and a UILabel.
The uilabel will show the number of stepper pressed.
My problem comes when i used core data to save the value of the uilabel. e.g. the final value of the uilabel is 30.
When i load back the data, the uilabel showed 30 but, when i press the stepper again, the uilabel reset to 1 again.
Is there any way to make the stepper continue to count based on my saved value?
Below is my code.
- (IBAction)stepperValueChanged:(id)sender
{
double stepperValue = ourStepper.value;
self.label.text = [NSString stringWithFormat:#"%.f", stepperValue];
}
- (IBAction)stepperValueChanged:(id)sender
{
NSString *tempString = [NSString stringWithFormat:#"30"];// you can pass here whatever data(stepper value) that you retrieve from core data...
double steppervalue = [tempString doubleValue];
double stepperValue = ourStepper.value+steppervalue;
self.label.text = [NSString stringWithFormat:#"%.f", stepperValue];
}
Hope, this will help you..

Add numerical content of UITextFields

I have four separate UITextFields and I want to add the numerical value of them all and then display the content within a UILabel, below is current code:
- (void)updateString {
self.string1 = textField1.text;
self.string2 = textField2.text;
self.string3 = textField3.text;
self.string4 = textField4.text;
self.string5 = textField5.text;
label.text = self.total; // total is an NSString and label is a UILabel
}
I am unable to add together the numerical values within each textField1/2/3... and store the value within total and then update the label. Any suggestions?
NSString has a method on it -intValue. That is what you want to use.
Check the section "Getting Numeric Values" in the NSString documentation
int totalValue = [textField1.text intValue] + [textField2.text intValue]...;
label.text = [NSString stringWithFormat:#"The total value is %d", totalValue];

Calculating enough text to fit within existing UILabel

I can't get some CoreText text wrapping code working for me; it's just too complicated. I'm going to try and go another route, which is to split my UILabel into two.
What I'm trying to achieve is to have my text appear to wrap around my fixed sized rectangular image. It'll always be the same dimensions.
So, when the UILabel next to the image fills up exactly, it'll create another UILabel below the image.
Now, how do I calculate the text in the first UILabel and have it fit nicely in the entire width of the UILabel, without being too short or cut off at the end?
Well, this ought to work to get the substring of the master string that will fit within the desired width:
//masterString is your long string that you're looking to break apart...
NSString *tempstring = masterString;
while (someLabel.bounds.size.width < [tempString sizeWithFont:someLabelLabel.font].width) {
NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[tempString componentsSeparatedByString:#" "]];
//Remove the last object, which is the last word in the string...
[tempArray removeLastObject];
//Recreate the tempString with the last word removed by piecing the objects/words back together...
tempString = #"";
for (int i=0; i < tempArray.count - 1; i++) {
tempString = [tempString stringByAppendingFormat:#"%# ", [tempArray objectAtIndex:i]];
}
//You must append the last object in tempArray without the space, or you will get an infinite loop...
tempString = [tempString stringByAppendingFormat:#"%#", [tempArray objectAtIndex:tempArray.count - 1]];
}
//Now do whatever you want with the tempString, which will fit in the width desired...
Of course, this is assuming you want the separation to occur using word wrapping. If you don't mind words themselves being cut apart (i.e. character wrap) in order to fully take up the desired width, do this instead:
NSString *tempstring = masterString;
while (someLabel.bounds.size.width < [tempString sizeWithFont:someLabelLabel.font].width) {
tempString = [tempString substringToIndex:tempString.length - 1];
}
//Now do whatever you want with the tempString, which will fit in the width desired...
In order to get the remaining piece of the string left over, do this:
NSString *restOfString = [masterString substringFromIndex:tempString.length];
Hope this helps. I have to admit that I haven't properly tested this code yet, though I've done something similar in the past...
Try below link its will help you.
If you want to create a "link" on some custom text in your label, instead of using a WebView as #Fabian Kreiser suggested, you sould use my OHAttributedLabel class (you can find it this link)
See the sample code provided on my github repository: you can use my addCustomLink:inRange: method to add a link (with a customized URL) to a range of text (range that you could determine by iterating over every occurrences of the word "iPhone" in your text very easily). Then in the delegate method on OHAttributedLabel, you can catch when the link is tapped and act accordingly to do whatever you need.

How do I Getting a TextField to do simple math?

I have three UITextFields. Two of them represent a certain number value. The third represents the percentage of the two. How do I setup the 3rd UITextField to do this simple math?
You can simply get the intValue or the floatValue or the doubleValue of the text that you have received from the first two text fields. Eg:
float firstFloat = [self.firstTextField.text floatValue];
float secondFloat = [self.secondTextField.text floatValue];
float answer = firstFloat / secondFloat; //or whatever math you need to do
self.thirdTextField.text = [NSString stringWithFormat:#"%.2f",answer];
Hope this helps.
Check out "Getting Numeric Values" here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
So you can use that to convert from the text property of your two UITextField instances.
Then you can convert them back to an NSString and plug them into the text of the third UITextField by using initWithFormat (something like [initWithFormat:#"%d", theResult]).
You can also do it in the following way.
NSString *str1,*str2;
str1=text1.text;
str2=text2.text;
int num1=[str1 intValue];
int num2 =[str2 intValue];
int ans=num+num2;
text3.text=[NSString stringWithFormat:#"%d",ans];
Hope this helps.
[answer setStringValue:[NSString stringWithFormat:#"%2.02g", answerFloat]];

Retrieve NSNumber From Array

I am relatively new to Objective C and need some array help.
I have a plist which contains a Dictionary and an NSNumber Array, with more arrays to
be added later on.
NSMutableDictionary *mainArray = [[NSMutableDictionary alloc]initWithContentsOfFile:filePath];
NSArray *scoresArray = [mainArray objectForKey:#"scores"];
I need to retrieve all the values from the array and connect them to 10 UILabels which
I've set up in interface builder. I've done the following to cast the NSNumber to a String.
NSNumber *numberOne = [scoresArray objectAtIndex:0];
NSUInteger intOne = [numberOne intValue];
NSString *stringOne = [NSString stringWithFormat:#"%d",intOne];
scoreLabel1.text = stringOne;
This seems a very long winded approach, I'd have to repeat the 4 lines above ten times to retrieve all the array values. Could I use a for loop to iterate through the array with all of the values converted to Strings at the output?
Any info would be greatly appreciated.
// create NSMutableArray* of score UILabel items, called "scoreLabels"
NSMutableArray *scoreLabels = [NSMutableArray arrayWithCapacity:10];
[scoreLabels addObject:scoreLabel1];
[scoreLabels addObject:scoreLabel2];
// ...
NSUInteger _index = 0;
for (NSNumber *_number in scoresArray) {
UILabel *_label = [scoreLabels objectAtIndex:_index];
_label.text = [NSString stringWithFormat:#"%d", [_number intValue]];
_index++;
}
EDIT
I'm not sure why you'd want to comment out _index++. I haven't tested this code, so maybe I'm missing something somewhere. But I don't see anything wrong with _index++ — that's a pretty standard way to increment a counter.
As an alternative to creating the scoreLabels array, you could indeed retrieve the tag property of the subviews of the view controller (in this case, UILabel instances that you add a tag value to in Interface Builder).
Assuming that the tag value is predictable — e.g., each UILabel from scoreLabel1 through scoreLabel10 is labeled with a tag equal to the values of _index that we use in the for loop (0 through 9) — then you could reference the UILabel directly:
// no need to create the NSMutableArray* scoreLabels here
NSUInteger _index = 0;
for (NSNumber *_number in scoresArray) {
UILabel *_label = (UILabel *)[self.view viewWithTag:_index];
_label.text = [NSString stringWithFormat:#"%d", [_number intValue]];
_index++;
}
The key to making that work is that the tag value has to be unique for the UILabel and must be something you can reference with -viewWithTag:.
The code above very simply assumes that the tag values are the same as the _index values, but that isn't required. (It also assumes the UILabel instances are subviews of the view controller's view property, which will depend on how you set up your interface in Interface Builder.)
Some people write functions that add 1000 or some other integer that allows you group types of subviews together — UILabel instances get 1000, 1001, and so on, and UIButton instances would get 2000, 2001, etc.
try using stringValue...
scoreLabel1.text = [(NSNumber *)[scoresArray objectAtIndex:0] stringValue];