Receiving value from one method to another - Objective C

Asked

Viewed 661 times

3

I need to receive the value that comes from the settings of the device (That 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 picking up the information that was set up on the device, so far so good, is working right. The problem is that I need to pass this variable 'txt' to another class (if I may call it that) Constants. h (It is in the folder called Bakershelf) and put in this code snippet:

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

Replacing the [txt] at the value received from the first.

Thank you.

  • 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.

  • 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?

  • 1

    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 the AppDelegate.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 in header.

  • 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?

  • Since I already have the string organized, there is no way I can do something like this: #define NEWSSTAND_MANIFEST_URL = variavel?

  • 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.

  • I created as static and the code went like this static NSString *NEWSSTAND_MANIFEST_URL = urlTxt. But it gives an error "Initializer element is not a Compile-time Constant"." From what I understand, this one Constants.his 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?

  • 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.

Show 3 more comments

3 answers

1

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!

  • There is also the use of Singleton. It may be interesting for you. Take a look at this link: http://answall.com/questions/25447/manter-objeto-vivo-na-memoria-para-outra-classe/25466#25466

0

#import <UIKit/UIKit.h>

@interface UIView ()

@end

NSString *Global_PESS_CODIGO;

#import <UIKit/UIKit.h>
#import "Constants.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

and in Appdelegate. m

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

Global_PESS_CODIGO = txt;

then it just matters #import "Constants.h" when you want to use the string

0

Why do you need to define a constant? Why not create a class with a method that returns the value?

Asim:

@interface Constants
+ (NSString *)newsstandManifestURLString;
@end

@implementation Constants
+ (NSString *)newsstandManifestURLString {
    NSString *txt = [[NSUserDefaults standardUserDefaults] objectForKey:@"txtEmail"];
    return [NSString stringWithFormat:@"[link_do_site]?digitha_userid=%@", txt];
}
@end

Browser other questions tagged

You are not signed in. Login or sign up in order to post.