2012-10-02 55 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

相關問題