I Have a textfield and I need the user to put a number in there, this number I need to transform into a float value and take it to another view. I can’t find my error… Xcode says it is on the “float valor = [[_inserir.text ] floatvalue];“line. If somebody could find where is my error, thanks.
first view .h:
#import <UIKit/UIKit.h>
@interface CedulasFirstViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *inserir;
- (IBAction)calc:(id)sender;
@end
first view .m: (the underline before my variable name, xcode put it by itself, taking it out doesn’t change anything)
#import "CedulasFirstViewController.h"
#import "CedulasSecondViewController.h"
@interface CedulasFirstViewController ()
@end
@implementation CedulasFirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)calc:(id)sender {
float valor = [[_inserir.text ] floatvalue];
CedulasSecondViewController *second [[[CedulasSecondViewController alloc] init]];
second.valor = self.valor;
}
@end
second view .h:
#import <UIKit/UIKit.h>
@interface CedulasSecondViewController : UIViewController{
}
@end
second view .m:
#import "CedulasSecondViewController.h"
@interface CedulasSecondViewController ()
@end
@implementation CedulasSecondViewController
- (void)viewDidLoad
{
NSString *numberFromTF [[NSString alloc] initWithFormat:@"%.2f", valor];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Thanks
EDIT:
if someone could explain me why xcode says to put this underline before the variable
There are a few things you need to do here.
First, you’ve created a property in
CedulasFirstViewController.h, so you need to synthesize it inCedulasFirstViewController.m. InCedulasFirstViewController.m, beneath the line@implementation CedulasFirstViewController, typeThen, in the line
float valor = [[_inserir.text ] floatvalue];, there are a couple errors. You’re treating your property like an instance variable, so instead of_inserir.textyou should useself.inserir.text. Also, you should remove the inner pair of brackets, because brackets are used for when functions are being performed, and there’s no function being performed inself.inserir.text.self.inserir.text(or_inserir.text, for that matter) is simply an object.All this means that the line producing the error should look like this:
There seem to be some other problems with this code, but if you ask about them those should be a separate question.