【发布时间】:2012-11-26 06:09:35
【问题描述】:
我熟悉 OOP,因为我已经用 Java 编码了一段时间,但是我在 Objective-C 中遇到了(语法?)问题。我一直在看这里的其他帖子,但到目前为止没有任何帮助。
我有一个名为“Play_Name.m”的类,它有一个方法可以在触摸按钮时设置玩家名称,还有另一个方法可以获取名称并返回一个字符串,(NSString*)get_name。我还有另一个名为“Play_ChooseChar.m”的类,它应该显示通过调用 get_name 函数输入的名称。
当我在“Play_Name”(它的所有者)中调用它时,get_name 返回正确的名称,但是当我在“Play_ChooseChar”中调用它时,它返回(null)。
//Play_Name code below
#import "Play_Name.h"
@interface Play_Name ()
@end
@implementation Play_Name
@synthesize playerName;
@synthesize textName;
-(IBAction)set:(id)sender {
[self setPlayerName:(self.textName.text)];
if([self.textName.text length] <= 0) {
playerName = @"Player";
NSLog(@"YOUR NAME: %@", playerName);
}
NSLog(@"YOUR NAME: %@", playerName);
}
//...........
@end
//Play_ChooseChar code below
#import "Play_ChooseChar.h"
#import "Play_Name.h"
@interface Play_ChooseChar ()
@end
@implementation Play_ChooseChar
@synthesize display_name;
@synthesize playname;
@synthesize boy;
@synthesize girl;
@synthesize isGirl;
@synthesize isBoy;
bool isGirl = FALSE;
bool isBoy = FALSE;
-(void)theName {
Play_Name *pN = [[Play_Name alloc] init];
[pN setPlayerName: pN.playerName];
NSLog(@"NAME: %@", pN.playerName);
self.display_name.text = pN.playerName;
//display_name.text = @"test";
[pN release];
//............
@end
所以当我运行它并输入我的名字时,来自“Play_ChooseChar”的打印语句返回'NAME: (null)'
【问题讨论】:
标签: objective-c oop instance