无法使标签接收字符串

时间:2013-01-10 02:06:27

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

修改

我在视图中有一个文本字段,一个按钮和一个标签,另一个视图上有一个标签,它们由不同的 viewControllers控制,在我的代码中我获取文本字段的浮点值和将其转换为字符串,然后我将此字符串放入两个标签中。但问题是,只有带有文本字段和按钮的视图上的标签才会收到它,而另一个视图上的另一个标签则不会收到任何内容!

first.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

first.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

second.h

#import <UIKit/UIKit.h>

@interface NotasSecondViewController : UIViewController{

    NSString *valorclasstwo;
    IBOutlet UILabel *classe2labelvalor;

}

@end

second.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

conectionsfirst

conectionssecond

3 个答案:

答案 0 :(得分:1)

首先检查您的插座是否设置正确。如果没问题,那么首先使用XIB给出label值,然后检查一个静态值。如果值得到正确的值,那么将值从一个视图控制器传递到另一个视图控制器时会出现问题。确保以适当的方式创建了property

@property (nonatomic, strong) NSString *strLabel;

然后@synthesize在.m文件中正确显示。如果您已完成此操作,请调试代码并将值传递给此视图控制器。

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

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

如果你这样做,你也会在另一个视图控制器中获得价值,你需要使用breakpoint调试你的代码。希望这会对你有所帮助。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

检查Interface Builder中的Label的绑定