编辑:
我在一个视图中有一个文本字段、一个按钮和一个标签,在另一个视图上有一个标签,它们由不同的视图控制器控制,在我的代码中,我获取文本字段的浮点值并将其转换为字符串,然后我把它串入两个标签。但问题是,只有带有文本字段的视图上的标签和按钮接收它,另一个标签,即在另一个视图上,不接收任何东西!
第一个.h
#import <UIKit/UIKit.h>
@interface NotasFirstViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *tfield;
- (IBAction)calcular:(id)sender;
-(IBAction)clicarFora:(id)sender;
-(IBAction)tirarteclado:(id)sender;
@property (strong, nonatomic) NSString *valorstr;
@property (strong, nonatomic) IBOutlet UILabel *mostradordeteste;
@end
第一个.m
#import "NotasFirstViewController.h"
#import "NotasSecondViewController.h"
@interface NotasFirstViewController ()
@end
@implementation NotasFirstViewController
@synthesize valorstr;
- (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 = [_tfield.text floatValue];
NSString *valorstr = [[NSString alloc] initWithFormat:@"%.2f", valor];
NSLog(@"%.2f", valor);
NSLog(valorstr);
_mostradordeteste.text = valorstr;
NotasSecondViewController *second = [[NotasSecondViewController alloc] init];
}
-(IBAction)clicarFora:(id)sender{
[_tfield resignFirstResponder];
}
-(IBAction)tirarteclado:(id)sender{
[sender resignFirstResponder];
}
@end
第二个.h
#import <UIKit/UIKit.h>
@interface NotasSecondViewController : UIViewController{
NSString *valorclasstwo;
IBOutlet UILabel *classe2labelvalor;
}
@end
秒.m
#import "NotasSecondViewController.h"
#import "NotasFirstViewController.h"
@interface NotasSecondViewController ()
@end
@implementation NotasSecondViewController
- (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.
}
-(void)teste{
NotasFirstViewController *n1 = [[NotasFirstViewController alloc] init];
valorclasstwo = [n1 valorstr];
classe2labelvalor.text = valorclasstwo;
}
@end