Objective c 目标C:方法和实例变量
PrefMySpotsViewCtrl.hObjective c 目标C:方法和实例变量,objective-c,xcode,methods,instance-variables,Objective C,Xcode,Methods,Instance Variables,PrefMySpotsViewCtrl.h @class Location; @interface PrefMySpotsViewCtrl : NSViewController { NSTextField *locationSearchInput; NSString * enteredLocation; Location *l; } @property (nonatomic, retain) IBOutlet NSTextField *locationSearchInput;
@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];
}
地点.h
@class PrefMySpotsViewCtrl;
@interface Location : NSObject
{
PrefMySpotsViewCtrl *p;
}
- (void) searchLocation;
地点
#import "Location.h"
#import "PrefMySpotsViewCtrl.h"
@implementation Location
- (void) searchLocation
{
NSLog(@"out of class: %@", [p enteredLocation]);
}
用户在locationSearchInput
中输入一个,以下是输出
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
方法永远不会执行
如果我做
l=[[Location alloc]init]
,然后执行searchLocation
,但输出为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)
有什么想法吗
谢谢?但是,问题是:您是否为location对象分配了一个有效的控制器实例(PrefMySpotsViewCtrl) 我的意思是:
l = [[Location alloc] init];
l->p = self;
[l searchLocation];
请记住,最好在位置声明中将PrefMySpotsViewCtrl声明为属性,如下所示:
@interface Location : NSObject
{
PrefMySpotsViewCtrl *p;
}
@property (nonatomic, assign) PrefMySpotsViewCtrl *p;
然后使用属性设置器指定它:
l = [[Location alloc] init];
l.p = self;
[l searchLocation];
编辑
从下面的评论来看,OP似乎不理解逻辑,因此我发布了一个简单的示例,让他更好地理解:
1) A类声明:
@interface ClassA : NSObject
@property(nonatomic,retain) NSString *ABC;
@end
2) B类声明:
@interface ClassB : NSObject
@property(nonatomic,assign) ClassA *p;
-(void) printClassAvar;
@end
@implementation ClassB
-(void) printClassAvar {
NSLog(@"Variable = %@", [self.p ABC]);
}
@end
3) 用法:
您还没有显示您的init方法 可能您还没有为
l
创建iVar。i、 例如:
// in the view controllers `initWithNibName:bundle:` method
l = [Location alloc] init]; // or whatever the inititializer for a Location object is.
因为您没有创建类型为
l
的对象,所以它是nil
(无论如何使用较新的LLVM编译器),并且它不接收消息,因此您的方法永远不会被调用。如果我创建l=[[Location alloc]init]代码>,然后执行searchLocation,但出于相同的原因输出为空?您是否为正在发送的enteredLocation
消息创建了iVar?enteredLocation
看起来像iVar。你真的把赋值放在任何地方了吗?enteredLocation=[LocationSearchInputStringValue]代码>我要退一步到这里,问你想做什么。因为您有一个位置对象,其iVar是PrefMySpotsViewCtrl
和一个PrefMySpotsViewCtrl
具有位置对象的iVar。将其声明为属性,然后将其作为属性访问,请参见我编辑的答案…@grep try仅打印p变量,如果为空,则您没有设置控制器,否则控制器的enteredLocation为空…它仍然为空。。您能否发布一个简单的示例,例如NSString ABC在ClassA中声明并分配了一个值@“XYZ”,然后用ClassB中的XYZ值记录这个ABC字符串?可能值得强调的是,这是使用ARC进行内存管理。我假设在1)下,我需要合成ABC;2)我需要@class PrefMySpotsViewCtrl;然后合成p;不明白3)我想在ClassA中分配XYZ值,为什么我需要ClassA*a=[ClassA new];?我想从ClassB打印,为什么需要ClassB*b=[ClassB new];?
// in the view controllers `initWithNibName:bundle:` method
l = [Location alloc] init]; // or whatever the inititializer for a Location object is.