2012-09-05 23 views
2

你好,我試圖從ViewController中的文本字段的輸入複製到我的AppDelegate。不,不是,反之亦然。從ViewController複製一個方法到AppDelegate - NULL

,因爲我試圖做到這一點,我的NSLog的給了我這樣的:

StruktonTest[8467:11603] (null) 

順便說一句,StruktonTest是我Testapp的名稱。

這裏是我的代碼,我在ViewController中獲取文本:

if ([[[self m_textfield] text] length] == 10) { 
m_textfield.text=[NSString stringWithFormat:@"%@", m_textfield.text]; 
NSLog(@"%@", m_textfield.text); 
telefoonnummer = m_textfield.text; 

我把它複製到我的AppDelegate.h文件是這樣的:

@interface LocationDelegate : NSObject <CLLocationManagerDelegate> 
{ 
UILabel * resultsLabel; 
NSString *phonenumber; 


} 

- (id) initWithLabel:(UILabel*)label; 
-(id)initWithDictionary:(NSDictionary *)dict; 
-(id)initWithName:(NSString *)aName; 
-(NSDictionary *)toDictionary; 

@property (nonatomic , retain) NSString *phonenumber; 

下面是從視圖控制器我的代碼,也許我在這裏做錯事:

@interface LocationTestViewController : UIViewController <CLLocationManagerDelegate> { 
IBOutlet UILabel * m_significantResultsLabel; 
IBOutlet UISwitch * m_significantSwitch; 
IBOutlet UISwitch * m_mapSwitch; 
IBOutlet MKMapView * m_map; 
IBOutlet UITextField * m_textfield; 
NSString *String; 
IBOutlet UILabel * mobielnummer; 
IBOutlet UILabel * deellocatie; 
IBOutlet UILabel * welldone; 
IBOutlet UILabel * locatiemaps; 
IBOutlet UILabel * mapslocatie; 
NSString *telefoonnummer; 



LocationDelegate * m_significantDelegate; 

// location objects 
CLLocationManager* m_gpsManager; 
CLLocationManager* m_significantManager; 
} 

-(IBAction) actionSignificant:(id)sender; 
-(IBAction) actionMap:(id)sender; 
-(IBAction) actionLog:(id)sender; 
-(IBAction)changrGreeting ; 



@property (retain, nonatomic) IBOutlet UILabel * m_significantResultsLabel; 
@property (retain, nonatomic) IBOutlet UISwitch * m_significantSwitch; 
@property (retain, nonatomic) IBOutlet UISwitch * m_mapSwitch; 
@property (retain, nonatomic) IBOutlet MKMapView * m_map; 
@property (nonatomic ,retain) IBOutlet UITextField *m_textfield; 
@property (nonatomic, copy) IBOutlet NSString *String; 
@property (nonatomic, retain) IBOutlet UILabel *mobielnummer; 
@property (nonatomic, retain) IBOutlet UILabel *deellocatie; 
@property (nonatomic, retain) IBOutlet UILabel *welldone; 
@property (nonatomic, retain) IBOutlet UILabel *locatiemaps; 
@property (nonatomic, retain) IBOutlet UILabel *mapslocatie; 
@property (nonatomic , retain) NSString *telefoonnummer; 



@end 

這是我的AppDelegate.m代碼

LocationTestViewController*locationTestViewController = [[LocationTestViewController alloc]init]; 
phonenumber = locationTestViewController.telefoonnummer; 
NSLog(@"%@", phonenumber); 

所以我收到NULL時,我複製此。 有誰知道如何解決這個問題。將不勝感激。

回答

0

此代碼:

LocationTestViewController*locationTestViewController = [[LocationTestViewController alloc]init]; 
phonenumber = locationTestViewController.telefoonnummer; 
NSLog(@"%@", phonenumber); 

是創造LocationTestViewController的新實例,然後讀取.telefoonnummer場這顯然是空的。

當然,而不是AppDelegate'閱讀'字段,你可以使用你的ViewController'給''字段到AppDelegete?

您可以使用作爲NSUserDefaults的臨時存儲,或在您的ViewController使用這樣的代碼:如果我在ViewController中使用的代碼

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; 
appDelegate.phoneNumber = self.telefoonnummer; 
+0

。它給我另一個號碼的錯誤。我已經在我的.h文件中定義了。或者你的代碼不是一個字符串? –

+0

對不起,您需要在您的ViewController的頂部爲您的AppDelegate添加一個#import,並將YourAppDelegate更改爲您的應用程序委託文件的名稱。 – Darren

+0

在'LocationDelegate'類型的對象上找不到屬性'phonenumber' –

相關問題