DanThePutzer DanThePutzer - 3 months ago 20
iOS Question

Read in float from UITextfield

I have been working on a little currency converter for iOS and wanted to read a float from a

UITextField
. Unfortunately I'm not able to read a float from the
UITextField
. I did some research on the Internet but none of the methods I tried worked. I am new to Objective-C and this is my first own post so feel free to give me feedback and advice for future posts too.

Here's the code in ViewController.m:

//
// ViewController.m
// Currencyz
//
// Created by Daniel Putzer on 21/08/16.
// Copyright © 2016 Daniel Putzer. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize Dollar, Euro, DollarValue, EuroValue, Result, ReadIn;

- (void)viewDidLoad {
[super viewDidLoad];

[self setDollar: 0];
[self setResult: @""];
}

- (IBAction) ReadEuro:(id)sender
{
Euro = [EuroValue.text floatValue];
}

- (void) Calculate
{
Dollar = 1.13245 * Euro;
Result = [NSString stringWithFormat: @"%.2f", Dollar];
}

- (IBAction)CalculateDollar:(id)sender
{
[self Calculate];
[DollarValue setText: Result];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


And here's ViewController.h:

//
// ViewController.h
// Currencyz
//
// Created by Daniel Putzer on 21/08/16.
// Copyright © 2016 Daniel Putzer. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
float Euro;
float Dollar;
NSString *Result;
NSString *ReadIn;

IBOutlet UILabel *DollarValue;
IBOutlet UITextField *EuroValue;
}

@property float Euro;
@property float Dollar;
@property NSString *Result;
@property NSString *ReadIn;

@property IBOutlet UILabel *DollarValue;
@property IBOutlet UITextField *EuroValue;

@end


I don't understand why it doesn't set the variable
Euro
to the amount entered in the
UITextField
.

Answer
- (void)viewDidLoad {

    [super viewDidLoad];

    [self setDollar: 0];
    [self setResult: @""];
    [self ReadEuro];
}

- (void) ReadEuro
{
    Euro = [EuroValue.text floatValue];
    NSLog("Euro value is : %0.02f", Euro);
}

try this and see if this prints your value.