使用指针在类之间进行通信

时间:2013-08-19 10:44:15

标签: objective-c class pointers cocos2d-iphone

大家好, 我很难理解如何使用指针 (我第一次使用它们)希望有人能指出我的权利 方向(没有双关语):P (它控制声音,pagenumber,bg,音频int和要显示的文本) (NSString *) textFromPlist 指针,指向包含 @“Page01Text”文本的plist 我想采用 @“Page01Text”并在 HighLighter类

中使用它

我能够在hello world层中记录声音,pagenumber,bg,audio int和text等的输出(它记录TextManagerWithpageNum)所以我知道plist正在读取, 但由于某种原因,我无法链接 textFromPlist 我希望能够使用textFromPlist类中的TextManagerWithpageNum并让hilighter类使用它(此类将hilight the text and work with plist提供的音频间隔) 它返回的这段代码(NULL)

    @interface HighLighter.h

    TextManagerWithpageNum *myClassAccess; 
    //declaring the TextManagerWithpageNum class so I can access textFromPlist


    @implementation HighLighter.m
     -(NSString *)Sentance01 
    {
    NSString *textFromTextManager = myClassAccess.textFromPlist;
    NSString *storyText = [NSString stringWithFormat:@"%@",textFromTextManager];
        return [[[NSString alloc]initWithString:StoryText]autorelease]; 
        //doesn't crash but doesn't load text

    NSLog(@"text form plist @%",storyText);

    }

我做错了什么? (我有一个int数组我也希望链接,但我想我会从NSString开始,因为我认为这会更简单。) 任何帮助都会很精彩,亲切的问候, 娜塔莉。

    @interface TextManagerWithpageNum : CCLayer
    NSString* textFromPlist_Pntr;
    @property(nonatomic, readonly) NSString* textFromPlist;
   //NSString* textFromPlist is an item on the plist  @"page01"  which contains text for  the page

   @property(nonatomic, readonly) NSString* audioInterval;
   //NSString* AudioInterval is an array on the plist  @"AudioTimings_PG01"

   @implementation TextManagerWithpageNum
   @synthesize textFromPlist = textFromPlist_Pntr;
   @synthesize audioInterval = _audioInterval;

1 个答案:

答案 0 :(得分:1)

在类之间传递任何数据时有几个选项。我将使用从SomeData发送到ObjectA的示例ObjectB对象来显示两者。您还可以使用类级方法,例如,在指向单例对象的指针中发送或接收数据。我不会告诉你如何做到这一点,因为它听起来不适合你的问题。

选项1

ObjectB中定义接收消息。

@interface ObjectA
{
    SomeData *someDataPointer;
    ObjectB *someObjectB;
}
@end

@implementation ObjectA

-(void)someMethod
{
    someDataPointer = [[SomeData alloc] init]; // Or whatever way you want to create and set someDataPointer
    [someObjectB receiveData:someDataPointer];

    // Note: if you will be calling receiveData on someObjectB much later than the
    //    time of creation of someDataPointer, you should retain someDataPointer to
    //    hold onto it until you need to pass it on.
    //    ie)
    // [someDataPointer retain];
}

@end

@interface ObjectB
-(void)receiveData:(SomeData *)dataPtr;
@end

@implementation ObjectB

-(void)receiveData(SomeData *)dataPtr
{
    // Here the data from someDataPointer will also be pointed to by dataPtr
    // Use dataPtr now
    [self doSomethingWithData:dataPtr];
}

@end

选项2

ObjectA中定义发送讯息。

@interface ObjectA
{
    SomeData *someDataPointer;
}

@property (nonatomic, retain) SomeData *someDataPointer;

@end

@implementation ObjectA
@synthesize someDataPointer = someDataPointer_;
@end

@interface ObjectB
{
    ObjectA *someObjectA;
}
@end

@implementation ObjectB

-(void)someMethod
{
    SomeData *dataPtr = [someObjectA getSomeDataPointer];
    // Now do something with dataPtr
}

@end

我希望有所帮助。我想向您展示这两种技术,以便您可以学习将其专门用于您的案例。如果您有疑问,请告诉我。