2012-01-29 116 views
0

我有一個字符串數據必須發送到另一個視圖控制器 - 在我的情況是一個tabbarcontroller。發送時我收到了空數據。 下面的代碼片段:將字符串從一個視圖控制器傳遞到另一個Tabbarcontroller

ViewController.h

#import <UIKit/UIKit.h> 
@class ViewController2; 

@interface ViewController1 : UIViewController{ 

@private 
ViewController2 *suiteNo; 

} 

@property(nonatomic,retain) ViewController2 *viewController2; 


-(IBAction)suiteNumber:(UIButton *)sender; 
@end 

我已經宣佈在.m文件如下

ViewController1.m

@synthesize viewController2; 

//data accessed 
self.viewController2.suiteNum = [[sender titleLabel] text]; 

ViewController2.h

#import <UIKit/UIKit.h> 



@interface ViewController2 : UIViewController{ 

NSString *suiteNum; 

    } 

@property(nonatomic,copy)NSString *suiteNum; 
@end 

ViewController2.m

@synthesize suiteNum; 

//the passed string is used here 
NSString *firstURL = [segmentOneURL stringByAppendingString:suiteNum]; 

是否有一個地方我犯了一個錯誤的任何地方?如果我錯了,請糾正我。謝謝。

回答

1

代替在viewcontroller2使得suiteNum屬性的,聲明suiteNum在的appdelegate和從viewcontroller1複製字符串值suiteNum

這樣的:= 的AppDelegate * appdelegate1 =(AppDelegate中*)[[UIApplication的sharedApplication]委託];

appdelegate1.suiteNum = [[sender titleLabel] text];

並且當viewcontroller2出現覆蓋它的viewWillAppear中委託作爲

的AppDelegate * appdelegate1 =(AppDelegate中*)[[UIApplication的sharedApplication]委託];

NSString * firstURL = appdelegate1.suiteNum;

+0

我試着在appdelegate中定義suiteNum屬性,然後導入appdelegate的.h文件。 我是否必須更改@property(nonatomic,copy)NSString * suiteNum;到別的東西? – 2012-01-29 15:00:38

+1

每當你訪問suiteNum總是使用此行前= appdelegate * appdelegate1 =(AppDelegate *)[[UIApplication sharedApplication]委託];然後使用appdelegate1.suitNum = [[sender titleLabel] text];然後在ViewController2中再次創建appdelegate實例,然後NSString * firstURL = appdelegate1.suiteNum; – 2012-01-29 15:18:24

+0

謝謝piyush。是否有可能編輯並在你的答案中添加這個?看起來有點難以閱讀語法。謝謝 – 2012-01-29 15:31:40

相關問題