Xcode Add (Math) Label and Textfield - xcode

Hi I am trying to add (math not put letters together) a textfield's data, and a label's data together. I have preformed the same code you see below for multiplication but when I do the same (A little changed) it does not seem to work. The code I placed below just shows the value of the billAmountTextField. Do you know why? Thanks for your help!
-(IBAction)totalAllUp:(id)sender
{
NSString *billTotalAmountString = [billAmountTextField text];
float billTotalFloat =[billTotalAmountString floatValue];
NSString *tipTotalAmountString = [resultLabel text];
float tipTotalAmountFloat = [tipTotalAmountString floatValue];
float totalAmountAfterAdding = billTotalFloat + tipTotalAmountFloat;
NSString *finalAnswer = [NSString stringWithFormat:#"%0.2f", totalAmountAfterAdding];
[totalAfterAdd setText:finalAnswer];
}

Related

How to use "valueforkey"?

I'm trying to do the following - I have an Array in which some strings are stored. These strings shall be used to call an NSArray. An example will clarify what I'm trying to do:
This is the working code that I'm trying to achieve ("briefing0" is of type NSArray):
NSString *path = [docsDir stringByAppendingPathComponent:[briefing0 objectAtIndex:indexPath.row]];
This is the "same" code that I'm trying to use:
int i = 0;
NSString *path = [docsDir stringByAppendingPathComponent:[(NSArray *)[NSString stringWithFormat:#"briefing%d", i] objectAtIndex:indexPath.row]];
Any ideas?
Thanks in advance!
Tom
Assuming that briefing0 is actually a property, then yes, this is possible (and not uncommon) in ObjC via KVC.
int i = 0;
NSString *prop = [NSString stringWithFormat:#"briefing%d", i];
NSArray *array = [self valueForKey:prop];
NSString *value = [array objectAtIndex:indexPath.row];
... etc. ...
-valueForKey: is the piece you're looking for. Note that this will throw an exception if you construct a key that does not exist, and so must be used with extreme care.

Xcode TextField Shows Numbers but Code Adds Decimal

I am making tip calculator and this is the code I currently have: (Below). [To make it look nice], is there a way for the user to just put in (15), and have code make that say .15 in the background? Thanks
- (IBAction)calcTapped:(id)sender {
NSString *billAmountString = [billAmountTextField text];
float billAmountFloat = [billAmountString floatValue];
NSString *tipPercentString = [tipPercentTextField text];
float tipPercentFloat = [tipPercentString floatValue];
float tipAmount = billAmountFloat * tipPercentFloat;
NSString *result = [NSString stringWithFormat:#"Tip: %0.2f",
tipAmount];
[resultLabel setText:result];
}
Try retrieving the string from the textfield, converting it into a NSNumber (or float) and then dividing by 100. You know, since 15/100 = 0.15.

How to convert String to double with point?

I'm currently working on an app that works with locations and therefore I have the following code:
CLLocationCoordinate2d coord;
coord.longitude = [longitude doubleValue];
coord.latitide = [latitude doubleValue];
Seems legit, doesn't it?
Now longitude and latitude are STRINGS like "10.112233". I know that normally double hasn't got points in there, but the cllocationcoordinate2d wants it like that...
Now, if you NSLog the strings, they work just fine, but if you NSLog the doubleValues it simply return nothing. How can I fix that?
The following should work:
NSString *coordinateAsString = #"10.112233";
coord.longitude = [[NSDecimalNumber decimalNumberWithString:coordinateAsString] doubleValue];
Can you try using NSDecimalNumber like following:
NSString *string = #"10.112233";
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:string];

Assigning NSString value to label

I have tried to apply the value from a NSString to a label in IB. The problem im seeing, is that the value assigne to the label is:
UITextField: 0x6d4e0f0; frame = (7 86; 97 31); text = '2'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; layer = CALayer: 0x6d4e210
I obviously would like to just see "2" rather than the whole UITextfield data.
Any clues?
In your label say:
[label setText: theTextField.text];
Instead of:
[label setText: theTextField];
My guess is your passing the whole UITextField object, but you really just want to pass the .text part of it.
For your comment:
NSString date = [NSString stringWithFormat:#"%#/%#/%#", day, month, year];

route-me image overlay in iPhone

I'm using route-me in my project. Library is quite good. Adding markers, drawing polygone work fine. How about placing single image as overlay in given location (latitude, longitude)? This funcionality is missing I think. Has anyone done placing the image overlay without overloading the tiles source?
I've found the solution...
CLLocationCoordinate2D lcA = CLLocationCoordinate2DMake(oSector.f_overlay_lat_min,oSector.f_overlay_long_min);
CLLocationCoordinate2D lcB = CLLocationCoordinate2DMake(oSector.f_overlay_lat_max,oSector.f_overlay_long_max);
CGPoint cgA = [mvMap latLongToPixel:lcA];
CGPoint cgB = [mvMap latLongToPixel:lcB];
float fLatMin = MIN(cgA.x,cgB.x);
float fLongMin = MIN(cgA.y,cgB.y);
float fWidth = sqrt((cgA.x - cgB.x)*(cgA.x - cgB.x));
float fHeight = sqrt((cgA.y - cgB.y)*(cgA.y - cgB.y));
RMMapLayer *mlLayer = [[RMMapLayer alloc] init];
mlLayer.contents = (id) oSector.im_overlay.CGImage;
mlLayer.frame = CGRectMake(fLatMin,fLongMin,fWidth,fHeight);
[[mvMap.contents overlay] addSublayer:mlLayer];
the mvMap is IBOutlet RMMapView *mvMap somewhere in your h file
the oSector.im_overlay.CGImage can be
UIImage *i = [UIImage imageNamed:<something>];
lmLayer.contents = i.CGImage
Why not just use an RMMarker? You can apply any image you want to it and place it as needed. Even make it draggable if you want to:
UIImage *imgLocation = [UIImage imageWithContentsOfFile :
[[NSBundle mainBundle] pathForResource:#"location_ind"
ofType:#"png"]];
markerCurrentLocation = [[RMMarker alloc] initWithUIImage:imgLocation];
// make sure it is always above everything else.
markerCurrentLocation.zPosition = -1.0;
[mapView.markerManager addMarker:markerCurrentLocation
AtLatLong:startingPoint];
Ciao!
-- Randy

Resources