从一个xib传递到另一个xib的值在Xcode 5中不起作用

时间:2013-10-10 08:10:24

标签: iphone ios

我有2个带有xib文件的类(不在storyboard中)。当我将一个值从一个类传递给另一个类时,它总是在Xcode 5之前工作。但是现在它不能用于Xcode 5.我将值从TimeZoneViewController传递给SignUpViewController,如下所示:

NSLog(@"value: %@",cell.textLabel.text); 
SignUpViewController *signUp = [[SignUpViewController alloc]initWithNibName:@"SignUpViewController" bundle:nil];
[signUp.timeZoneLabel setText:cell.textLabel.text];
[self.navigationController pushViewController:signUp animated:YES];   

日志显示的是值,但在SignUpViewController出现后,它在“timeZoneLabel”中没有显示任何内容。问题出在哪儿?提前谢谢。

4 个答案:

答案 0 :(得分:1)

在设置timeZoneLabel的值之前,您的代码会将您导航到下一个屏幕。因此,您应该将第3行与第4行交换为首先设置timeZoneLabel的文本,然后导航到SignUpViewController

使用此代码:

NSLog(@"value: %@",cell.textLabel.text); 
SignUpViewController *signUp = [[SignUpViewController alloc]initWithNibName:@"SignUpViewController" bundle:nil];
[signUp.timeZoneLabel setText:cell.textLabel.text];
[self.navigationController pushViewController:signUp animated:YES];  

更新:

1)在SignUpViewController

中创建一个属性
@property (strong, nonatomic) NSString * strTimeZoneLabel;

2)在第一个视图控制器中,设置该属性值。

NSLog(@"value: %@",cell.textLabel.text); 
SignUpViewController *signUp = [[SignUpViewController alloc]initWithNibName:@"SignUpViewController" bundle:nil];
signUp.strTimeZoneLabel = cell.textLabel.text;
[self.navigationController pushViewController:signUp animated:YES];

3)在-viewDidLoad的{​​{1}}中,设置SignUpViewController的文字:

timeZoneLabel

答案 1 :(得分:1)

SignUpViewController的视图未从xib加载到内存中,因此signUp.timeZoneLabel == nil。使用controller属性发送值:

signUp.timeZoneLabelText = cell.textLabel.text;

这是您的财产:

@property (strong, nonatomic) NSString * timeZoneLabelText;

这是实现方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.timeZoneLabel setText:self.timeZoneLabelText];
}

答案 2 :(得分:1)

更改代码,添加NSString并更新如下:

OtherView.h

@property (nonatomic, strong) NSString *myText;

OtherView.m

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.timeZoneLabel setText:self.myText];
}

传递数据:

NSLog(@"value: %@",cell.textLabel.text); 
SignUpViewController *signUp = [[SignUpViewController alloc]initWithNibName:@"SignUpViewController" bundle:nil];
[signUp.myText setText:cell.textLabel.text];
[self.navigationController pushViewController:signUp animated:YES];  

答案 3 :(得分:1)

您无法设置另一个视图控制器的标签文本,因此您必须将字符串值从一个类传递到另一个类,并在signupviewcontroller的viewDidLoad方法中将此字符串值分配给label。

喜欢以下代码

在SignUpViewController.h文件中

@property (nonautomic, retain) NSString *strTimeZone;
@property (nonautomic, retain) UILabel *lblTimeZone;

在SignUpViewController.m文件中

- (void)viewDidLoad
{
     [super viewDidLoad];
     self.lblTimeZone.text = strTimeZone;
}

在firstviewcontroller中您想要推送另一个类的文件

NSLog(@"value: %@",cell.textLabel.text); 
SignUpViewController *signUp = [[SignUpViewController alloc]initWithNibName:@"SignUpViewController" bundle:nil];
signUp.strTimeZone = cell.textLabel.text;
[self.navigationController pushViewController:signUp animated:YES];  

尝试这样你的问题就会解决