-4

Subclass ViewController.m Code:

@interface ViewController : NSViewController 
@property (assign) IBOutlet NSArrayController *objectArrayController;
@property (assign) IBOutlet NSTableView *objectTable;
@property (weak) IBOutlet NSImageCell *objectImageCell;
@end
  • And ViewController.m contains the implementation Pretty standard
  • How exactly do I access these properties from a separate class?
  • Do I just include the 'ViewController.m' file, or do I declare setter/gettor methods somewhere in the main.m file which I can access from any file in my project?
Virbhadrasinh
  • 529
  • 6
  • 19
Gab
  • 19
  • 1
  • 8

2 Answers2

0

You need to get the pointer to the specific instance of the view controller that contains the property you want to reference. (It will not work to alloc/init a new copy of the VC and attempt to access the property there.)

Once you have the pointer it's simple:

SomeType* theCopiedData = theSourceVCPointer.thePropertyIWant;

Of course, getting that pointer is sometimes a challenge -- it actually requires thought and planning in many cases.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
-1

You need to do two thing to access the properties from another class.

  1. You need to make sure you synthesized these properties as well. You didn't show it, but I'm going to assume you did.

  2. You need to get the instance of the view controller you want to access inside your other class.

     ViewController *controller =  (ViewController*)self.window.rootViewController;
    

This code is for your root view only.

Chase
  • 2,206
  • 1
  • 13
  • 17
  • Wow...someone went freakin crazy with the down votes. Kind of useless not adding input or anything. Anyways. Im understanding what you guys are saying exactly my problem is getting the instance of this view controller. @Chase there is no such property "rootViewController" – Gab Feb 04 '15 at 23:43
  • Lol don't worry about the down votes. I don't let them bother me. There is a property https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIWindow_Class/index.html#//apple_ref/occ/instp/UIWindow/rootViewController – Chase Feb 05 '15 at 00:14
  • Try this solution to get the root view controller http://stackoverflow.com/questions/12418177/how-to-get-root-view-controller – Chase Feb 05 '15 at 00:18
  • You have to make sure you've assigned a root view in your AppDelegate.m as well – Chase Feb 05 '15 at 00:20
  • Well nothings working. Ive looked everywhere and nothings working for setting a rootViewController I have no idea how. Everything I see is with the UIKit, not NS. I got the properties to recognize in my other class by using contentViewController, but they NSTextFields always return null despite having values. – Gab Feb 05 '15 at 02:17
  • Basically I have 4 NSWindowControllers connected to NSViewControllers and those 4 NSViewControllers have my ViewController class. They each have certain NSTextfields on them which I'd like to be able to access from anywhere in the program. – Gab Feb 05 '15 at 02:55