将方法从ViewController复制到AppDelegate - NULL

时间:2012-09-05 12:14:01

标签: ios nsstring null viewcontroller appdelegate

您好我正在尝试将ViewController中文本字段的输入复制到AppDelegate。 不,反之亦然。

因为我正在尝试这样做而且我在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;

这是来自ViewController的我的代码,也许我在这里做了一些错误:

@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。 有谁知道如何解决这个问题。将不胜感激。

1 个答案:

答案 0 :(得分:0)

此代码:

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

正在创建LocationTestViewController的新实例,然后读取显然为空的.telefoonnummer字段。

当然,不是AppDelegate“读取”该字段,您可以使用ViewController将该字段“提供”给AppDelegete吗?

您可以将NSUserDefaults用作临时存储,或者在ViewController中使用这样的代码:

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