objective-c – Getting value from one method to another – Objective C


I need to receive the value that comes from the device settings (which I already got, as it is in the code below) and I need to pass this return to a string that will allow login to my application, take a look at the code:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString* txt = [defaults objectForKey:@"txtEmail"];

The code above is in AppDelegate.m (Inside the folder called Baker), and that's why I'm getting the information that was configured on the device, so far so good, it's working fine. The problem is that I need to pass this 'txt' variable to another class (if I can call it that) called Constants.h (It's in the folder called BakerShelf) and put in this code snippet:

#define NEWSSTAND_MANIFEST_URL @"[link_do_site]?digitha_userid=[txt]

Replacing the [txt] with the value received from the first snippet.



Friend, I had a problem similar to yours, I was trying to store data generated in a class and pass it to another. The solution I found was as follows:

Go to the class that will receive the data, and create a variable that will store the data and its property. Then also create a method that takes as argument the data you want to store, and inside the method make it be stored in the variable you created.

In the fileOriginData.m, place the #import "ClassQueArmazena.h" and instantiate an object of the class that will receive the data, and pass the value you want in the method argument.

As in the example code that I'm going to do below to be better illustrated.

*** ClasseQueRecebeDados.h ***


@interface ClasseQueRecebeDados : NSObject{

 NSString* receptor;


@property(nonatomic, retain) NSString* receptor;



In the fileClassWhoReceivesData.m you implement this method as follows:

#import "ClasseQueRecebeDados.h"

@implementation ClasseQueRecebeDados

@syntesize receptor;


 self.receptor = dadosRecebido;


Now in ClasseQueProneceData.m you do like this:

// Importe a classe para instanciar um objeto dela e ter acesso ao seu método.
    #import "ClasseQueRecebeDados.h"

      @implementation ClasseQueForneceDados


     ClasseQueRecebeDados* armazenador = [[ClasseQueRecebeDados alloc]init];

      // Aqui você instancia a classe ClasseQueRecebeDados e usa o método.
     [armazenador metodoTransfereDados: self.dadoQueSeráEnviadoHaOutraClasse];



When using the method, you will send the data to the variables of the object you instantiated. Within the method, I believe it will be more useful to store the data in a plist, than having it only in the app's memory, this way the data will be visible from various points in the app.

Hope this has helped, any questions just comment below!

Scroll to Top