1

I've googled around and found some answers but I didn't get any of them to work. I have one NSObject with the class "A" and a second class "B" without an NSObject. In class "A" are my IBOutlets defined and I can't seem to figure out how to access those outlets from class "B"...

I've found answered questions like http://forums.macrumors.com/archive/index.php/t-662717.html But they're confusing.

Any help would be greatly appreciated!

Simplified Version of the Code:

aClass.h:

#import <Cocoa/Cocoa.h>

@interface aClass : NSObject {
    IBOutlet NSTextField *textField;
}
@end


aClass.m:

#import "aClass.h"

@implementation aClass
// Code doesn't matter
@end


bClass.h:

#import <Cocoa/Cocoa.h>

@interface bClass : NSObject {
}
@end


bClass.m:

#import "aClass.h"
#import "bClass.h"

@implementation bClass
    [textField setStringValue: @"foo"];
@end
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Jef
  • 297
  • 5
  • 15

2 Answers2

2

When you write:

I have one NSObject with the class "A" and a second class "B" without an NSObject.

It tells me that you don't have your head around the basic concepts. Read through Apple's objective-C introduction, and the tutorial projects.

NSResponder
  • 16,861
  • 7
  • 32
  • 46
  • I am reading http://www.amazon.com/Programming-Objective-C-2-0-Stephen-Kochan/dp/0321566157 I guess it will come down to the same eventually. – Jef Dec 20 '09 at 19:24
1

The solution is using NSNotificationCenter. Here's a thread telling you how to do it: Send and receive messages through NSNotificationCenter in Objective-C?

Then in the method reacting to the notification, you call a method accessing the Outlet

- (void) receiveTestNotification:(NSNotification *) notification
{

    if ([[notification name] isEqualToString:@"TestNotification"])
        //NSLog (@"Successfully received the test notification!");
        [self performSelectorOnMainThread:@selector(doIt:) withObject:nil waitUntilDone:false];
}
- (void) doIt
{
    //testLabel.text = @"muhaha";
}

This worked for me, I hope it does so for you as well.

Community
  • 1
  • 1
Erik
  • 2,138
  • 18
  • 18