-1

PrefMySpotsViewCtrl.h

@class Location;

@interface PrefMySpotsViewCtrl : NSViewController
{
  NSTextField *locationSearchInput;
  NSString * enteredLocation;

  Location *l;
}

@property (nonatomic, retain) IBOutlet NSTextField *locationSearchInput;
@property (nonatomic, retain) NSString *enteredLocation;

PrefMySpotsViewCtrl.m

#import "Location.h"


- (void) controlTextDidChange:(NSNotification *)aNotification
{
   enteredLocation = [locationSearchInput stringValue];
   NSLog(@"in class:%@", enteredLocation);
   [l searchLocation];
}

Location.h

@class PrefMySpotsViewCtrl;

@interface Location : NSObject

{
  PrefMySpotsViewCtrl *p;  
}

- (void) searchLocation;

Location.m

#import "Location.h"
#import "PrefMySpotsViewCtrl.h"

@implementation Location

- (void) searchLocation
{
   NSLog(@"out of class: %@", [p enteredLocation]);
}

User inputs a into locationSearchInput and here is the output

2012-09-30 10:18:12.915 MyApp[839:303] in class:
2012-09-30 10:18:12.917 MyApp[839:303] in class:a

searchLocation method is never executed.

If I do l = [[Location alloc] init]; , then searchLocation is executed but the output is null

2012-09-30 10:28:46.928 MyApp[880:303] in class:
2012-09-30 10:28:46.929 MyApp[880:303] out of class: (null)
2012-09-30 10:28:46.930 MyApp[880:303] in class:a
2012-09-30 10:28:46.931 MyApp[880:303] out of class: (null)

Any idea?

Thanks?

Bruno von Paris
  • 882
  • 1
  • 7
  • 26
grep
  • 566
  • 5
  • 20

2 Answers2

2

But, the question is : have you assigned a valid instance of the controller(PrefMySpotsViewCtrl) to the location object ?

I mean :

l = [[Location alloc] init];
l->p = self;
[l searchLocation];

Keep in mind that is better to declare the PrefMySpotsViewCtrl as a property in the Location declaration, something like the following :

@interface Location : NSObject
{
  PrefMySpotsViewCtrl *p;  
}
@property (nonatomic, assign) PrefMySpotsViewCtrl *p;

And then assign it using the property setter :

l = [[Location alloc] init];
l.p = self;
[l searchLocation];

EDIT

Since from the comments below seems that the OP didn't understand the logic, i post a simple example to let him understand better :

1) ClassA declaration :

@interface ClassA : NSObject
@property(nonatomic,retain) NSString *ABC;
@end

2) ClassB declaration:

@interface ClassB : NSObject 
@property(nonatomic,assign) ClassA *p;
-(void) printClassAvar;
@end

@implementation ClassB
-(void) printClassAvar {
    NSLog(@"Variable = %@", [self.p ABC]);
}
@end

3) Usage :

ClassA *a = [ClassA new];
a.ABC = @"XZY";
ClassB *b = [ClassB new];
b.p = a;
[b printClassAvar];
aleroot
  • 71,077
  • 30
  • 176
  • 213
  • Declare it as property and then access it as a property, see my edited answer ... – aleroot Sep 30 '12 at 08:56
  • @grep try print only the p variable, if it is null you don't have set the controller otherwise the enteredLocation of your controller is null ... – aleroot Sep 30 '12 at 09:10
  • it is still null .. would you be able to post a simple example of say NSString ABC declared in ClassA and assigned a value @"XYZ" and then NSLog this ABC string with XYZ value from ClassB? – grep Sep 30 '12 at 09:27
  • It might be worth stressing that this is using ARC for memory management. – Abizern Sep 30 '12 at 09:59
  • i assume under 1) I need to synthesize ABC; under 2) I need @class PrefMySpotsViewCtrl; and then synthesize p; do not not understand 3) I want to assign XYZ value in ClassA, why do I need ClassA *a = [ClassA new];? I want to print from ClassB, why do I need ClassB *b = [ClassB new];? – grep Sep 30 '12 at 10:02
  • @grep It depends on the compiler you use, with the latest version no, otherwise yes. – aleroot Sep 30 '12 at 10:03
  • I am using XCODE with ARC on MBP 2011 – grep Sep 30 '12 at 10:07
  • @grep the example is clear ... If you don't understand it, you better learning objective-C first ... – aleroot Sep 30 '12 at 10:10
1

You haven't shown your init method.

It is possible that you haven't actually created an iVar for l. i.e something like:

// in the view controllers `initWithNibName:bundle:` method
l = [Location alloc] init]; // or whatever the inititializer for a Location object is.

Because you haven't created an object of type l it is nil (with the newer LLVM compiler anyway), and it doesn't receeive messages, so your method is never called.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • If I do `l = [[Location alloc] init];` , then searchLocation is executed but the output is null – grep Sep 30 '12 at 08:53
  • For the same reason? Have you created an iVar for `p` that you are sending the `enteredLocation` message? – Abizern Sep 30 '12 at 08:54
  • `enteredLocation` looks like an iVar. Are you actually putting assigning it a value anywhere? – Abizern Sep 30 '12 at 09:00
  • `enteredLocation = [locationSearchInput stringValue];` – grep Sep 30 '12 at 09:05
  • I'm going to step back here and ask what it is that you are trying to do. Because you have a location object with an iVar that is a `PrefMySpotsViewCtrl` and a `PrefMySpotsViewCtrl` that has an iVar of a Location object. – Abizern Sep 30 '12 at 09:07
  • I simply need to access `enteredLocation` from `Location` class – grep Sep 30 '12 at 09:09
  • Then you need to set the property in location to be the instance of PrefMySpotsViewCtrl that actually has an entered location. Although, tbh, this looks a bit convoluted to me. – Abizern Sep 30 '12 at 09:13
  • would you be able to post a simple example of say NSString ABC declared in ClassA and assigned a value @"XYZ" and then NSLog this ABC string with XYZ value from ClassB? – grep Sep 30 '12 at 09:29
  • It's a bit trivial -> `NSLog(@"%@",a.stringProperty);` – Abizern Sep 30 '12 at 09:43