0

编辑:

我在一个视图中有一个文本字段、一个按钮和一个标签,在另一个视图上有一个标签,它们由不同的视图控制器控制,在我的代码中,我获取文本字段的浮点值并将其转换为字符串,然后我把它串入两个标签。但问题是,只有带有文本字段的视图上的标签和按钮接收它,另一个标签,即在另一个视图上,不接收任何东西!

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

连接优先

连接秒

4

3 回答 3

0

查看两个标签的出口是否正确连接(黑色圆圈)。如果圆圈没有被填满,那么就有问题了。

于 2013-01-10T02:16:59.420 回答
0

在 Interface Builder 中检查 Label 的绑定

于 2013-01-10T05:16:02.197 回答
0

首先检查您的插座设置为标签是否正确。如果没问题,那么首先使用 XIB 给出标签值并检查一个静态值是否到来。如果值你得到正确的值,那么在将值从一个视图控制器传递到另一个视图控制器时就会出现问题。确保您property以正确的方式创建。

@property (nonatomic, strong) NSString *strLabel;

然后将@synthesize其正确放入 .m 文件中。如果你已经完成了这件事,那么调试你的代码并将值传递给这个视图控制器。

还有一件事像这样从第一个视图控制器设置您的值。

WebLinkViewController *sampleView = [[WebLinkViewController alloc] init] ;
sampleView.stringTitle = strTitle;
[self.navigationController pushViewController:sampleView animated:YES];

如果您以这种方式进行操作,您还将在另一个视图控制器中获得价值,您需要使用breakpoint. 希望这会帮助你。

于 2013-01-10T05:40:37.013 回答