dinesh
dinesh

Reputation: 837

How can I assign values to other class variable in objective-c

The below coding is working and I can see the values in my second screen. But I am using the same in other classes with different variables in this format. But it dosent show me the variable if after i type the classname with a dot. I cant figure this out. Is there any way to pass values to other class.

        InstallProfiler_2 *installProfiler2 = [[InstallProfiler_2 alloc] initWithNibName:@"InstallProfiler_2" bundle:nil];

        installProfiler2.profilerType2 = profilerType;

        [self.navigationController pushViewController:installProfiler2 animated:NO];
        [installProfiler2 release];

Upvotes: 2

Views: 4109

Answers (3)

Paul.s
Paul.s

Reputation: 38728

Make sure that:

  • You have imported the class header.
  • The @property declarations are in this header and not a class extension.

@property refers to ivars so when you say

if after i type the classname with a dot

this terminology is incorrect, you probably mean after you start typing the name of the variable which has points to an instance of a class.

ClassA.h

@interface ClassA : NSObject

@property (nonatomic, weak) NSInteger myInt;

@end

ClassA.m

@implementation ClassA

@synthesize myInt = _myInt;

@end

ClassB.m

#import "ClassA.h"   // <- Import the header of the class

@ implementation ClassB

//  .. other methods and stuff

- (void)myMethod;
{
     ClassA *instanceOfClassA = [[ClassA alloc] init];   // <- Working with an instance not a class
     instanceOfClassA.myInt = 1;
}

@end

UPDATE

Make sure your @property () does not have readonly between the round brackets. Also make sure you have either @synthesize'd the ivar in the implementation or have provided both a getter and a setter for the ivar.

Failing that show some relevant code so we can actually see what your doing - we are answering pretty blindly here.

Upvotes: 2

coneybeare
coneybeare

Reputation: 33101

Create a custom setter/getter:

+ (BOOL)awesomeClassVar {
    return _classVar;
}

+ (void)setAwesomeClassVar:(BOOL)newVar {
    _classVar = newVar;
}

then call as a method from the other class:

BOOL theOtherClassVar = [AwesomeClass awesomeClassVar];
[AwesomeClass setAwesomeClassVar:!theOtherClassVar];

Upvotes: 0

KIDdAe
KIDdAe

Reputation: 2722

The dot syntax is only available with property/synthesize

Upvotes: 0

Related Questions