Buddy, I’ve been having a similar problem with you, I was trying to store data generated in one class and move on to another. The solution I found was the following:
Go to the class that will receive the data, and create a variable that will store the data and its property. Then create also a method that takes as argument the data you want to save, and within the method make it be stored in the variable you created.
In the file Classeorigemdados. m, put the #import "Classequestor. h" and instate a class object that will receive the data, and pass the value you want in the method argument.
As in the example code I will do below to get better illustrated.
*** ClasseQueRecebeDados.h ***
import<UIKit/UIKit.h>
@interface ClasseQueRecebeDados : NSObject{
NSString* receptor;
}
@property(nonatomic, retain) NSString* receptor;
-(void)metodoTransfereDados:(NSString*)dadosRecebido;
@end
In the Received Classfile. m you implement this method as follows:
#import "ClasseQueRecebeDados.h"
@implementation ClasseQueRecebeDados
@syntesize receptor;
-(void)metodoTransfereDados:(NSString*)dadosRecebido{
self.receptor = dadosRecebido;
}
@end
Now in Classequeceddados. m you do so:
// Importe a classe para instanciar um objeto dela e ter acesso ao seu método.
#import "ClasseQueRecebeDados.h"
@implementation ClasseQueForneceDados
-(void)DentroDeAlgumMetodoDaClasseQueForneceDados{
ClasseQueRecebeDados* armazenador = [[ClasseQueRecebeDados alloc]init];
// Aqui você instancia a classe ClasseQueRecebeDados e usa o método.
[armazenador metodoTransfereDados: self.dadoQueSeráEnviadoHaOutraClasse];
}
(DEMAIS CODIGO DA IMPLEMENTAÇÂO ...)
@end
When using the method, you will send the data to the variables of the object you requested. Inside the method, I believe it will be more useful to save the data in a plist, than to have it only in the memory of the app, this way the data will be visible from various points of the app.
I hope it has helped, any doubt just comment below!
Actually as it is a constant, you cannot change the value of it, because at the time of setting you do not have the value of "txt", you would have to use the value, create a new Nsstring by giving replace in the placeholder. Check out http://stackoverflow.com/questions/668228/string-replacement-in-objective-c.
– Wakim
Thanks for the help, this will be useful, but my difficulty is in bringing the variable from one file to another. A variable is declared in Appdelegate. m, and I need to use it in Constants.h. You know how I can do this?
– Diego Neri
From what I understand of the semantics of the file
Constants.h
is that other files use it, not the other way around. Soon you would have to include in it (Contants.h
) from within theAppDelegate.m
(take a look at http://stackoverflow.com/questions/439662/what-is-the-difference-between-import-and-include-in-objective-c) and you can use everything declared inheader
.– Wakim
Great! I was able to get the values and put them in the string. But from what you said then I won’t be able to put that string back in that code snippet
#define NEWSSTAND_MANIFEST_URL @"[link_do_site]?digitha_userid=[txt]
? Because I need my altered string to go back to Constants. h to be used in this #define, there’s no way I can do it?– Diego Neri
Since I already have the string organized, there is no way I can do something like this:
#define NEWSSTAND_MANIFEST_URL = variavel
?– Diego Neri
I know two ways: Instead use defines, declare it as
static
(having a template using define). Or put it in the user preferences after replacing.– Wakim
I created as
static
and the code went like thisstatic NSString *NEWSSTAND_MANIFEST_URL = urlTxt
. But it gives an error "Initializer element is not a Compile-time Constant"." From what I understand, this oneConstants.h
is a file that will be read before the execution of the program, so everything should be static, and not to use variables. I would have some suggestion how to solve this problem?– Diego Neri
Thanks for the help, I managed to solve the problem by creating a global variable with the string, and giving replace in the parts I needed to edit, worked smoothly.
– Diego Neri