Objective C Accessors

Discussion in 'Mac Applications and Mac App Store' started by ag2web, Nov 14, 2012.

  1. macrumors newbie

    ag2web

    Joined:
    Mar 9, 2011
    #1
    I am struggling to understand some basic concepts in Objective C, would appreciate if someone can help. This is about accessors - setters and getters. I have Cocoa application with the following classes:

    //AppDelegate.h
    #import <Cocoa/Cocoa.h>
    @interface AppDelegate : NSObject <NSApplicationDelegate>
    @property (nonatomic, retain) IBOutlet NSTextView *drawerText;
    @end

    //AppDelegate.h
    @implementation AppDelegate
    //some methods here
    @end

    //Another.h
    #import <Cocoa/Cocoa.h>
    @interface Another : NSViewController
    //some properties here
    @end


    //Another.m
    #import "AppDelegate.h"
    @implementation Another
    - (void) someMethod {
    AppDelegate *appD = [AppDelegate new];
    NSTextView *drawerText2 = [[NSTextView alloc] init];
    drawerText2.string = @"ABC";
    appD.drawerText = drawerText2;
    NSLog(@"out %@", appD.drawerText.string);
    }
    @end

    I guess the issue I have should be understandable from what I am trying to do in Another.m someMethod. In summary:

    - Theres NSTextView *drawerText defined in AppDelegate
    - drawerText is an IBOutlet which is properly linked in IB
    - I need to display some content in drawerText and the content should come from Another.m class
    - When I run someMethod nothing is displayed in drawerText
    - when I NSLog drawerText from Another.m it has ABC value
    - when I NSLog drawerText from AppDelegate.m it is empty, though not (null)
    - when I run the same someMethod from within AppDelegate.m, everything work, i.e. ABC is displayed in drawerText

    As I said, I believe I am missing some basic understanding here, as the issue is not only with this particular drawerText but with any control or instance variable defined in one class and set / get from another class.

    I read a lot of Objective C docs by Apple, various books (Stephen G. Kochan, SCOTT KNASTER, MARK DALRYMPLE, etc) but I just can't grasp how to use setters and getters across different classes.

    I do not have any programming background, Objective C is my first language. Usually, if there is enough time, I can figure out things, but not this time. By reading these books, I even managed to developed couple of simple Cocoa Apps, but had to keep everything in one class, which obviously is not correct, especially when an app becomes more or less complex.

    Any help will be appreciated.
     

Share This Page