2 つのビューと 2 つのクラスがあります。最初のクラスによって制御される最初のビューには、テキストフィールドとボタンがあります。2 番目のクラスによって制御される 2 番目のビューには、2 つのラベルがあります。テキスト フィールドに入力した値を一方のラベルに受け取り、もう一方のラベルにこの値を 100 で割った値を受け取るようにしようとしました。
ファーストクラス.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
セカンドクラス.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 に正しく接続されています。