1

i have two .m file .i used nsstring object on another .m file its always null.

//postputgetFunction.h

     @property(retain,nonatomic) IBOutlet NSMutableString *postRegisterResponseUserId;

//postputgetFunction.m

  @synthesize postRegisterResponseUserId;

    -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
   {
if ([flag isEqualToString:@"post"])
{
    NSLog(@"Post received data here.....");
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    postRegisterResponseName=[dict valueForKey:@"Name"];
    postRegisterResponseSuccess=[dict valueForKey:@"Success"];
    postRegisterResponseUserId=[dict valueForKey:@"UserId"];

    NSLog(@"ReceiveData :Name : %@ \n Success : %@ \n UserId : %@",postRegisterResponseName,postRegisterResponseSuccess,postRegisterResponseUserId);

   //Above statement display the value properly..........

    flag=Nil;
}
}

but i am using in another .m file ... In this .m file its shown null value .. like this, //Verification.h

    #import "PostPutGetFunction.h"

    @property (retain, nonatomic) PostPutGetFunction *postputgetFunction;

//verification.m

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
postputgetFunction=[[PostPutGetFunction alloc]init];

}

- (IBAction)verificationBtnClick:(id)sender
{
    NSLog(@"%@",postputgetFunction.postRegisterResponseUserId);

    //here its always shown NULL ... i didnt get the value here ...
}
MAC113
  • 1,444
  • 12
  • 19
  • Since you seem to get the String asynchronously, I assume you are accessing the string before the callback has fired. Where are you sending the request? – 最白目 Dec 05 '13 at 09:08
  • PostPutGetFunction class contain two metho post and put ..first post method calling from login form and store value in string and navigate to verification xib. after that verification class's button click it execute verificatinBtnClick action in that get the value of userid – MAC113 Dec 05 '13 at 09:22
  • still not sure but I stick with the asynchronous problem. Your sending the request -> navigate to verification.xib -> get the value of User ID (still NULL here) -> the server now answers witth the callback -> now the User ID is set. The request is asynchron, means: after the reqeust is sent, the rest of the code gets executed "along next to the request". – 最白目 Dec 05 '13 at 09:40

2 Answers2

1

In other .m file's viewDidLoad method you allocating and initializing the PostPutGetFunction using

postputgetFunction=[[PostPutGetFunction alloc]init];

That's why the variable defined in PostPutGetFunction class NSMutableString *postRegisterResponseUserId initialized to Null. You can use the Delegates for passing the data between the two controllers. Or alternatively you can store the userID in NSUserDefault class like below

**First Part**
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *name =[dict valueForKey:@"Name"];
NSString *success=[dict valueForKey:@"Success"];
NSString *userid =[dict valueForKey:@"UserId"];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

[defaults setObject:name forKey:@"NAME"];
[defaults setObject:success forKey:@"SUCCESS"];
[defaults setObject:userid forKey:@"USERID"];
[defaults synchronize];

And to retrieve the values in another class.m use below code

**Second Part**
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSString *name = [defaults objectForKey:@"NAME"];
NSString *success = [defaults objectForKey:@"SUCCESS"];
NSString *userid = [defaults objectForKey:@"USERID"];

And also you do not use IBOutlet for NSMutableString type rather it's used for UI Control types like Below

Gyanendra Singh
  • 1,483
  • 12
  • 15
  • let me google this for you: http://iosdevelopertips.com/objective-c/java-developers-guide-to-static-variables-in-objective-c.html – 最白目 Dec 05 '13 at 09:48
  • @MAC113 see my edited answer I think your sole objective is to get the values in other .m file. This is very straight forward approach as compared to static type. – Gyanendra Singh Dec 05 '13 at 09:50
  • yes i want to get the value in other page but please in your above code you write two times and i didnt get what i used exactly in my code ... – MAC113 Dec 05 '13 at 10:32
  • please write again what i required exactly – MAC113 Dec 05 '13 at 10:35
  • In postputgetFunction.m connection:(NSURLConnection *)connection didReceiveData:(NSData *)data method where your are getting the values of name,success, userid use the First Part to set the values in NSUserDefault and In other.m file use the Second Part to get values what you have stored previously I hope this would help you. – Gyanendra Singh Dec 05 '13 at 10:42
  • when i write the second part in other.m file ... its automatically get the value ... no need to used object of postputgetFuncrtion class – MAC113 Dec 05 '13 at 10:57
  • @GyanendraSingh thank you ... its working and i got the value on other.m file – MAC113 Dec 05 '13 at 11:28
  • NSUserDefaults is singleton class that you can use to store primitive data type (like user preferences etc) and can be accessed anywhere within the app so you do not need to create the object of PostputgetFuncrtion. – Gyanendra Singh Dec 05 '13 at 11:30
0

You are Declaring a new object of PostPutGetFunction, and for sure the value postRegisterResponseUserId will be null

if you want to accomplish that you have to use Delegation, take a look at this answer

Community
  • 1
  • 1
Mutawe
  • 6,464
  • 3
  • 47
  • 90