【问题标题】:Copying a Method from ViewController to AppDelegate - NULL将方法从 ViewController 复制到 AppDelegate - NULL
【发布时间】:2012-09-05 12:14:01
【问题描述】:

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

【问题讨论】:

    标签: ios nsstring null viewcontroller appdelegate


    【解决方案1】:

    这段代码:

    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;
    

    【讨论】:

    • 如果我在 ViewController 中使用您的代码。它给了我电话号码的另一个错误。我已经在我的 .h 文件中定义了。还是您的代码不再是字符串?
    • 抱歉,您需要在 ViewController 的顶部为您的 AppDelegate 添加一个#import,并将 YourAppDelegate 更改为您的应用委托文件的名称。
    • 在“LocationDelegate”类型的对象上找不到属性“phonenumber”
    • 根据您上面的代码,您已将其声明为@property (nonatomic , retain) NSString *phonenumber;
    • 对不起,我没有意识到你会复制和粘贴。将 N 更改为 n
    猜你喜欢
    • 1970-01-01
    • 2018-09-26
    • 2014-11-18
    • 1970-01-01
    • 2019-03-05
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多