0

I have created a custom class of NSObject, and I would like to access some instance variables from my main View Controller to that custom class, how do you do that?

EDIT: Perhaps I was unclear in my first formulation. It is the instance variables from the ViewController class I would like to access, not the ivars from my custom class.

1
  • As i understand you want to access the same class variable into different place? or what kinda value you are tying to access? Commented Jul 7, 2014 at 13:20

3 Answers 3

1

If I got you right, the simplest way is to pass view controller instance during initialization. Just implement initWithViewController:(UIViewController*)vc in your custom class.

Sign up to request clarification or add additional context in comments.

Comments

0

You should expose your data as properties in your NSObject subclass. You can find a description of properties at this tutorial: http://rypress.com/tutorials/objective-c/properties.html.

Comments

0

You should better use properties to easy access to some info in your custom class. They automatically generate iVars.

Also you can access public (declared in .h file) ivars directly:

@interface CustomClass : NSObject
{
    NSArray *_array1;
}

@property (nonatomic, strong) NSArray *array2;



CustomClass *instance = [CustomClass new];

NSArray *a1 = instance->_array1;
NSArray *a2 = instance.array2;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.