2

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

头等舱.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

第一类.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

二等舱.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

二等舱.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:我所有的 Outlets 都正确连接到 UILabel。

4

2 回答 2

0

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

尝试在其中设置断点

-(IBAction)calcular:(id)sender

确保其实际被调用的方法

于 2013-01-12T23:50:55.687 回答
0

最佳的揣测:

看起来你是在被调用resultadoFloatValor之后设置viewDidLoad的。

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

于 2013-01-12T23:52:44.920 回答