2

您好,我正在尝试将 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。有谁知道如何解决这个问题。将不胜感激。

4

1 回答 1

0

这段代码:

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

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

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

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

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.phoneNumber = self.telefoonnummer;
于 2012-09-05T12:22:45.153 回答