How can I access a value in a class which was set in another class in objective c? - objective-c

I have two classes, classA and classB.
I navigated from classA to classB.
I set a value
Temp = 0; in classB.
I popped back to classA from classB.
Now, I need to access Temp value in classA.
how can I get that without setting the value in AppDelegate calss ?

You should use a Property. Have a look at the apple tutorial
#interface MyClass : NSObject
{
NSString *value;
}
#property(copy, readwrite) NSString *value;
#end
#implementation MyClass
#synthesize value;
#end
In another class that has a instance of MyClass
myInstanceOfMyClass.value = #"hi"; // Sets the value to 'hi'
NSString* myString = myInstanceOfMyClass.value; // Gets the value :)

I guess, there are two ways to do this:
Create owner object in class B and initialize it with class A's object. When done with class B, set the value in class A's variable and then use it into class A
Create a property in App Delegate and assign this variable from Class B. When back to class A, use the variable from App Delegate.

Related

Access class methods from instance of other class in Objective-C

Let's say I have class A and class B. In class A I create an instance of class B. Class B has delegate methods that, when called, I want to call instance methods in class A. Is there a way to pass a reference to class A when instantiating class B?
You can use a delegate to return information to the first Class
Class B.h
#protocol delegateB <NSObject>
-(void)doSomething:(NSString *)data;
#end
#interface ClassB : UIViewController
#property(nonatomic, assign) id<delegateB> delegate;
#end
Class B.m
#synthesize delegate = _delegate;
- (void)viewDidLoad {
[_delegate doSomething:#"String to send"];
}
In the class A:
Class A.h
#interface ClassA : UIViewController
-(void)doSomething:(NSString *)data;
#end
Class A.m
When you instance the class, you need to assign self:
ClassB *cb = [[ClassB alloc] init];
cb.delegate = self;
To use the function:
-(void)doSomething:(NSString *)data{
//do something whit the data
}
Add a property of type A to B and assign self to it after initialising. You could also pass self as an argument to the delegate method you mentioned. Actually this is common practice in cocoa (look at UITableViewDelegate)
You can use type Class to pass a reference to a class. For class A use [A class] to get the referenece. But note that with a general class reference you won't be able to simply write [aClassRef aClassMethod], because Class instancce can reference any class (so the compiler don't know if particular method is supported or not).
The actual solution should be closer to what Julian suggests, but maybe this note will be useful in some way too.

What is the difference between declaring a member in the extended interface versus in the implementation?

I am seeing two very different behaviors for something that I thought were the exact same.
Defining my private member in the class extension like this:
#interface ClassA ()
#property ClassB* b;
#end
#implementation ClassA
-(ClassA*)initWithClassB:(ClassB*)newB
{
self.b = newB;
return self;
}
-(ClassB*)getB
{
return self.b;
}
Or defining my private member in the class implementation like this:
#interface ClassA ()
#end
#implementation ClassA
ClassB* b;
-(ClassA*)initWithClassB:(ClassB*)newB
{
b = newB;
return self;
}
-(ClassB*)getB
{
return b;
}
The way I am using this code is to create a ClassB object, initialize a ClassA object with that ClassB object, and then add the ClassA object to a mutable array
-(void)init
{
self.classAList = [[NSMutableArray alloc] init];
[self.classAList addObject:[[ClassA alloc] initWithClassB:[self createClassB1]]];
[self.classAList addObject:[[ClassA alloc] initWithClassB:[self createClassB2]]];
[self.classAList addObject:[[ClassA alloc] initWithClassB:[self createClassB3]]];
}
-(ClassB)createClassB1
{
ClassB* classB = new ClassB();
//do some init
return classB;
}
// Same thing fore createClassB2 and createClassB3 except with different data
When I use the first approach, and define my member in the interface extension, I see that each element in my mutable array is indeed what I would expect.
However, using the second approach, I see that my ClassB* b pointer in the ClassA object always ends up pointing to the most recently created ClassB object. That is, once the -(void)init method finishes, the ClassB pointers in each of the ClassA objects points to the ClassB object I created in createClassB3
What is happening here?
I should also mention that the ClassB object is a C++ object and this is a an objective-c++ class.
In your second snippet, b is just a global variable at file scope. The fact that it's inside of the #implementation ... #end is irrelevant. It is not an instance variable nor a property.
With the second approach you're creating a global variable, meaning it's not related to any instance of ClassA, so you will always have one and the same instance of *b pointing to the same object in memory. So anytime you change the value of the *b you're changing the object in memory at which the b variable is pointing, but never creating a new one; to understand it better you're basically initialising every ClassA object with the same ClassB variable (which is *b), so if you change the value at the portion of memory to which *b is pointing you're changing it for all the instances of ClassA created.
Hope it's clear enough.

Static attribute, Static Class, Singleton Pattern

I have 3 class.
Class A contains :
A static variable "dataX".
A setter method to set the data.
A getter method to return the data value.
Class B
Class C.
the flow is as follows:
The Class B instanciates the Class A and initialize the variable "dataX" with the setter method.
Afterwards, the class C instantiates the Class A in the viewDidLoad method and gets the value of the static variable.
But even if the variable in Class A is static, the variable is always null.
I guess that I need to put the Singleton Pattern with a static Class A and not simply a static attribute.
What is the syntax to specify a Class as Static?
The code below:
// HandleMessage.h
#interface HandleMessage : NSObject
#property *NSString nameFile;
// Getter
- (NSString *)getNameFile;
// Setter
- (void)setNameFile: (NSString *) value;
#end
And:
// HandleMessage.m
#import "HandleMessage.h"
#implementation HandleMessage
static nameFile;
#synthesize nameFile ;
// Getter definition
- (NSString *)getNameFile{
return nameFile;
}
// Setter definition
- (void)setNameFile: (NSString *) value{
nameFile = value;
}
When you instantiate another instance of a class of course this instance's value is null.
You can work with singletons or store your data elsewhere (if you want to keep data between app starts in your user defaults using NSUserdefaults)
what do you mean by "Class as Static"??
you can use singleton pattern, which i described in this answer
or using class method
ClassA.h
#interface ClassA
+ (void)setData:(int)data;
+ (int)getData;
#end
ClassA.m
static int sData;
#implementation ClassA
+ (void)setData:(int)data {
sData = data;
}
+ (int)getData {
return data;
}
#end

Accessing Sub class member from base class in objective c

i tried to access the subclass member variable from base class. But i can't access it. It returns a null value..
for eg..
I have baseclass classA and Subclass ClassB. i tried to access the classB member variable say x from base class ClassA. but it returns null. i assigned classB variable with some value.
i tried like this..
In classA.h
#classB
#property (nonatomic, retain) classB *BObj;
In classA.m
#import classB.h
#synthesize BObj;
BObj = [[classB alloc]init];
NSLog(#"%d",BObj.x); //returns NULL
In ClassB.h
#interface ClassB :ClassA
#property(nonatomic,retain) int x;
In ClassB.m
#synthesize x;
x = 10; //This value should be read from super class(Class A).
As Chuck says in comments, this doesn't look like real code. Where are you setting x to 10? If it isn't in your init method, or in a method called from init, it won't display. And you have invalidated your complaint with your edit - logging %d will not show null, it will show zero.
Try this : NSLog(#"%d", BObj.x);

Messaging between two classes

I have a basic question about fetching values through different classes.
I have a classA which fills an array (If i print it out it is not empty).
LATER in class B i want to load this Array: I call a function from class A which returns the Array of class A. But in class B if i call my new array then is it null.
I am a bit confused, because i think i retain every value of the array, but its still null. I tried also a lot of different possibilities. I think its a basic OOP syntax fault i produce?!
//CLASS_A.h
#interface classA {
NSMutableArray* buoyArray;
}
#property (nonatomic, retain) NSMutableArray * buoyArray;
-(NSMutableArray*)getArray:(NSMutableArray*)_array;
//CLASS_A.m
...
-(NSMutableArray*)getArray:(NSMutableArray*)_array {
_array=buoyArray;
return _array;
}
//CLASS_B.h
#import "CLASS_A.h"
#class classA;
#interface classB ...
classA *mapSource;
NSMutableArray * buoyArray;
}
#property(nonatomic,retain) classA *mapSource;
//CLASS_B.m
buoyArray=[mapSource getArray:buoyArray];
NSLog(#"%#",buoyArray);
Actually you you are making new object of class A by calling alloc so by init it reintialize all properties values for that instance.
What you need,if you are pushing class B over class A then, fetch existing class A object from stack, by using this line.
mapSource = (ClassA *)[self.navigationController.viewControllers objectAtIndex: [self.navigationController.viewControllers count]-2];
then call this
buoyArray=[mapSource getArray:buoyArray];
NSLog(#"%#",buoyArray);