2012-10-02 17 views
0

我必须将字符串值从一个视图传递到其他视图中的标签。但是当我做这样的事情时,标签值是空的。无法理解我出错的地方?这里是我的代码:当在iphone的其他视图中传递一个视图的字符串值时返回null?

在FirstViewController.m

SecondViewController *gp=[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]]; 
gp.d.text = [NSString stringWithFormat:@"%@", nam]; 
[self.view addSubview:gp.view]; 
[gp release]; 

在SecondViewController.h

@interface SecondViewController : UIViewController<NSXMLParserDelegate> 
{ 
    UILabel *d; 
} 

@property (nonatomic,retain) IBOutlet UILabel *d; 

在SecondViewController.m @synthesize d;

- (void)viewDidLoad 
{ 
    NSString *urlString1 = [NSString stringWithFormat: @"http://192.168.0.108:8732/Design_Time_Addresses/ICloudServices/AppointmentService/json/GetProviders/?cid={%@}", [d text]]; 
} 

但是这里d的值没有通过。为什么我不明白。我哪里错了?

+0

使用NSString将值传递给第二个viewController。 –

+0

视图控制器IBOutlet内存得到改变。所以使用NSString来传递值 –

回答

0

在添加子视图未分配(没有内存引用)之前,将值传递给IBOutlet UILablel时。

所以更好的方法是在添加子视图后传递您的值。

[self.view addSubview:gp.view]; 
gp.d.text=[NSString stringWithFormat:@"%@",nam]; 

编辑:更好的办法是通过字符串是这样的:

Create property of NSString in SecondViewController say yourString. 

现在

SecondViewController *gp=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]]; 
gp.yourString = [nam retain]; 
[self.view addSubview:gp.view]; 

现在SecondViewController的viewDidLoad中做到这一点:

self.d.text = self.yourString; 

根据您的要求在任何地方使用yourString。

+0

嘿我在一个标签中得到它,但是当我尝试在DidLoad()中的服务url中使用相同的值时,则不会传递该值。 Iam正在做什么...在SecondViewController.m中 - (void)viewDidLoad { NSString * urlString1 = [NSString stringWithFormat:@“http://192.168.0.108:8732/Design_Time_Addresses/ICloudServices/AppointmentService/json/GetProviders/? cid = {%@}“,[d text]];在这里,我无法得到d的价值。你能告诉我这是吗? – cutiepie

+0

查看编辑答案.... –

+0

@王子...非常感谢。我明白了。 – cutiepie

相关问题