Receiving value from one method to another - Objective C

3

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

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

The above code is in the AppDelegate.m (Inside the folder called Baker), and it is by that stretch that I am picking up the information that has been configured on the device, so far so good, it is working right. 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 stretch.

Thank you.

    
asked by anonymous 24.06.2014 / 22:55

3 answers

1

Friend, I was with a problem similar to yours, was trying to store data generated in one class and move to another. The solution I found was the following:

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

In the FileNameData.m file, place the #import "ClassNameStore.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 I'll 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 file ClassRouteData.m you implement this method as follows:

#import "ClasseQueRecebeDados.h"

@implementation ClasseQueRecebeDados

@syntesize receptor;

-(void)metodoTransfereDados:(NSString*)dadosRecebido{

 self.receptor = dadosRecebido;
}

@end

Now in ClassWhatMakeData.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 you use 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 save the data in a plist, than to have it only in the app's memory, so the data will be visible from various points in the app.

I hope you have helped, any questions just comment below!

    
30.07.2014 / 18:55
0

Why do you need to set 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
    
24.07.2014 / 14:19
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 just import #import "Constants.h" when you want to use the string

    
24.07.2014 / 07:22