geeker
geeker

Reputation: 239

Objective-C properties of another class to be accessed in other methods

I know that this question has been asked many a times in this forum, After going through those posts I did find a partial solution for the problem from one of the post, but I do have one doubt from this post Objective-C: Accessing a property of another class.

I was able to access the values of the properties by the given answer, but was not able to use the same values of those properties in other instance methods of the child class. Can anybody give some examples on how to do the same.

Update of the code

@interface ClassA : SomeSuperClass

@property (some Attributes) ClassB *classB;
@property (some Attributes) NSString *someString;

@end


@implementation

-(id)init {
    if (self = [super init]) {
        _classB = [[ClassB alloc]initWithParent:self];
    }
}

@end

@class ClassA;
@interface ClassB : SomeSuperClass

@property (nonatomic, weak) ClassA *classA;

-(id)initWithParent:(ClassA*)parent;

@end

#import "ClassA.h"
@implementation 
-(void)viewDidLoad{
    NSLog(@"%@",self.classA.someString); //here I get null
}
-(id)initWithParent:(ClassA*)parent {
    if (self = [super init]) {
        _classA = parent;
        NSLog(@"%@", self.classA.someString); //perfectly legal and prints the string value
    }
}

Upvotes: 1

Views: 205

Answers (3)

vishal
vishal

Reputation: 328

I think you can use a singleton where it creates a single instance, and other class can use the same instance for accessing the properties.

Ex:

+(id)singletonInstance
{
  static classA *classA = nil;
  static dispatch_oce_t onceToken;
  dispatch_once(&onceToken, ^{
  // if the instance is not there then create an instance and init one.
  classA = [[self alloc] init];
  });
  return classA;
}


// in the same class .m file viewDidLoad add the below code

//classA.m

classA *classA = [classA sharedInstance]; // this will be the instance which will be called by other classes (i.e classB ..etc).

Upvotes: 1

Nullable
Nullable

Reputation: 994

There are a lot of problems in your code, please check the code carefully.

I infer that SomeSuperClass probably inherits from UIViewController

1. Initialization does not return an instance object

All initialization methods should have a return value, the code you provide has no return value, you should change to this

- (instancetype)init{
    if (self = [super init]) {
        _classB = [[ClassB alloc]initWithParent:self];
    }
    return self;
}

2. weak modifier property

Generally, only weak is used to solve circular references. It should be avoided at other times and will be released immediately after use.

3. someString property does not see initialization

4. The order in which ClassA and ClassB enter

I assume that you solved the previous problem, and the order should be to enter ClassA first, then enter ClassB.

To enter ClassB from ClassA, you must use the initialized classB property, like this

ClassA.m

[self.navigationController pushViewController:self.classB animated:YES];

Then finally execute viewDidLoad when ClassB is displayed, you can get the correct someString

Upvotes: 0

Andrey Chernukha
Andrey Chernukha

Reputation: 21808

Change this

@property (nonatomic, weak) ClassA *classA;

to

@property (nonatomic, strong) ClassA *classA;

The reason why you are getting nil is because ClassA object is deallocated. It is deallocated because your weak reference does not retain it. Only strong references retain objects. Read about ARC.

Change ClassA implementation to the following:

@interface ClassA : SomeSuperClass

@property (some Attributes) ClassB *classB;
@property (some Attributes) NSString *someString;

@end


@implementation

-(id)init {
if (self = [super init]) {
    _classB = [[ClassB alloc]initWithParent:self];
}
}

- (void)dealloc
{
    // do you see this printed in console when you run the app?
    NSLog(@"DEALLOC!!!");
}

@end

Upvotes: 1

Related Questions