Ios 将方法从ViewController复制到AppDelegate-NULL

Ios 将方法从ViewController复制到AppDelegate-NULL,ios,nsstring,null,viewcontroller,appdelegate,Ios,Nsstring,Null,Viewcontroller,Appdelegate,您好,我正在尝试将输入从ViewController中的文本字段复制到AppDelegate。 不,反之亦然 当我尝试这样做时,NSLog给了我以下信息: StruktonTest[8467:11603] (null) 顺便说一句,Strukontest是我的Testapp的名称 以下是我在ViewController中获取文本的代码: if ([[[self m_textfield] text] length] == 10) { m_textfield.text=[NSString stri

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

当我尝试这样做时,NSLog给了我以下信息:

StruktonTest[8467:11603] (null)
顺便说一句,Strukontest是我的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;
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.phoneNumber = self.telefoonnummer;
我将其复制到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;
所以当我复制这个的时候,我收到了空值。 有人知道怎么解决这个问题吗。非常感谢。

此代码:

LocationTestViewController*locationTestViewController = [[LocationTestViewController   alloc]init];
phonenumber = locationTestViewController.telefoonnummer;
NSLog(@"%@", phonenumber);
正在创建LocationTestViewController的新实例,然后读取明显为空的.telefoonnumer字段

当然,您可以使用ViewController将字段“赋予”给AppDeleget,而不是AppDelegate“读取”字段

您可以将NSUserDefaults用作临时存储,也可以在ViewController中使用如下代码:

if ([[[self m_textfield] text] length] == 10) {
m_textfield.text=[NSString stringWithFormat:@"%@", m_textfield.text];
NSLog(@"%@", m_textfield.text);
telefoonnummer = m_textfield.text;
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.phoneNumber = self.telefoonnummer;
此代码:

LocationTestViewController*locationTestViewController = [[LocationTestViewController   alloc]init];
phonenumber = locationTestViewController.telefoonnummer;
NSLog(@"%@", phonenumber);
正在创建LocationTestViewController的新实例,然后读取明显为空的.telefoonnumer字段

当然,您可以使用ViewController将字段“赋予”给AppDeleget,而不是AppDelegate“读取”字段

您可以将NSUserDefaults用作临时存储,也可以在ViewController中使用如下代码:

if ([[[self m_textfield] text] length] == 10) {
m_textfield.text=[NSString stringWithFormat:@"%@", m_textfield.text];
NSLog(@"%@", m_textfield.text);
telefoonnummer = m_textfield.text;
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.phoneNumber = self.telefoonnummer;

如果我在ViewController中使用您的代码。它给了我另一个关于电话号码的错误。我已在.h文件中定义了。或者你的代码不再是字符串了?对不起,你需要在ViewController顶部为你的AppDelegate添加一个导入,并将你的AppDelegate更改为你的AppDelegate文件的名称。在类型为“LocationDelegate”的对象上找不到属性“phonenumber”,根据你上面的代码,你已经声明了它
@Property(非原子,保留)NSString*phonenumber;
抱歉,我没有意识到您会复制和粘贴。如果我在ViewController中使用您的代码,请将N更改为a。这会在电话号码上给我另一个错误。我已在.h文件中定义了。或者,它不再是代码中的字符串了吗?抱歉,您需要在ViewController顶部为您的AppDelegate添加一个#导入,并将您的AppDelegate更改为您的应用程序代理文件的名称。在“LocationDelegate”类型的对象上未找到属性“phonenumber”。根据上面的代码,您已将其声明为
@Property(非原子,保留)NSString*phonenumber;
抱歉,我没有意识到您会复制和粘贴。请将N更改为N