为什么Label不接收字符串

时间:2013-01-12 23:43:11

标签: iphone ios objective-c xcode cocoa-touch

我有两个视图和两个类。在第一个视图,由第一个类控制我有一个文本字段和一个按钮。在由第二类控制的第二个视图中,我有两个标签。我试图让一个标签接收我放在文本字段上的值,另一个标签这个值除以100.但在这两种情况下我得到0.00,我不知道为什么!

firstclass.h

#import <UIKit/UIKit.h>

@interface NotasViewController : UIViewController{

}

-(IBAction)calcular:(id)sender;
-(IBAction)clicarFora:(id)sender;
-(IBAction)recuarTeclado:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *inserirTF;


@end

fisrtclass.m

#import "NotasViewController.h"
#import "resultadoViewController.h"

@interface NotasViewController ()

@end

@implementation NotasViewController
@synthesize inserirTF;

- (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)calcular:(id)sender{

    float valor = [inserirTF.text floatValue];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    resultadoViewController *second = [storyboard instantiateViewControllerWithIdentifier:@"resultado"];
    second.resultadoFloatValor = valor;
    NSLog(@"%.2f", second.resultadoFloatValor);
}

-(IBAction)recuarTeclado:(id)sender{

    [sender resignFirstResponder];

}

-(IBAction)clicarFora:(id)sender{

    [inserirTF resignFirstResponder];

}

@end

secondclass.h

#import <UIKit/UIKit.h>

@interface resultadoViewController : UIViewController{

    float resultadoFloatValor;

}

@property float resultadoFloatValor;
@property (strong, nonatomic) IBOutlet UILabel *resultadoLabelValor;
@property (strong, nonatomic) IBOutlet UILabel *resultadoLabelQtd100;

@end

secondclass.m

#import "resultadoViewController.h"

@interface resultadoViewController ()

@end

@implementation resultadoViewController
@synthesize resultadoFloatValor, resultadoLabelValor, resultadoLabelQtd100;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSString *resultadoStringValor = [[NSString alloc] initWithFormat:@"%.2f", resultadoFloatValor];
    NSLog(@"%.2f", resultadoFloatValor);
    NSLog(@"%@", resultadoStringValor);
    resultadoLabelValor.text = resultadoStringValor;

    int resultadoIntResto100 = resultadoFloatValor/100;

    if (resultadoIntResto100 > 0) {

        NSString *resultadoStringQtd100 = [[NSString alloc] initWithFormat:@"%i", resultadoIntResto100];
        resultadoLabelQtd100.text = resultadoStringQtd100;

    }

}

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

@end

您将在我的代码中看到3个NSLog。为了测试,我把54放在我的文本字段上。输出是这样的:

2013-01-12 21:15:27.020 Divisorreal[21843:c07] 54.00
2013-01-12 21:15:27.023 Divisorreal[21843:c07] 0.00
2013-01-12 21:15:27.023 Divisorreal[21843:c07] 0.00

PS:我的所有奥特莱斯都正确连接到UILabels。

2 个答案:

答案 0 :(得分:1)

最佳猜测:

看起来您在调用resultadoFloatValor后设置了viewDidLoad

实施prepareForSegue:并在那里设置值。

答案 1 :(得分:0)

确保将UITextView的委托设置为将这些操作发送到控制器。

尝试在

中添加断点
-(IBAction)calcular:(id)sender

确保实际被调用的方法