1

我正在使用 Objective-C 制作 iOS SpriteKit“GameScene”文件。我有一个 SKLabelNode 和一个 UITextField 来输入玩家的名字。

如何使 UITextField 值等于将 SKLabelNode 更改为说“谢谢”+ UITextField 值?

(我的代码):

#import "GameScene.h"

@implementation GameScene

-(void)didMoveToView:(SKView *)view {
// Use Objective-C to declare all SpriteKit Objects
// inside the {parameters} of "didMoveToView" method:


// SKLabelNodes (SpriteKit Object) display "text".
// Objective-C object named *winner = SKLabelNode (SpriteKit Object)
SKLabelNode *winner = [SKLabelNode labelNodeWithFontNamed:@"Tahoma"];
winner.text = @"You Win!";
winner.fontSize = 65;
winner.fontColor = [SKColor blueColor];
winner.position = CGPointMake(500,500);
[self addChild:winner];


// UITextFields (SpriteKit Object) allow "text" input from users.
// Objective-C object named *textField = UITextField (SpriteKit Object)
UITextField *textField = [[UITextField alloc]
initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
textField.center = self.view.center;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textColor = [UIColor blackColor];
textField.font = [UIFont systemFontOfSize:17.0];
textField.placeholder = @"Enter your name here";
textField.backgroundColor = [UIColor whiteColor];
textField.autocorrectionType = UITextAutocorrectionTypeYes;
textField.keyboardType = UIKeyboardTypeDefault;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.view addSubview:textField];

}

-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}

@end
4

1 回答 1

1

您需要使用委托方法。具体来说

- (void)textFieldDidEndEditing:(UITextField *)textField; 

要在创建 textFeild 时执行此操作,请添加这行代码

[textfield setDelegate:self]

还要添加您在 .h 文件中的委托

@interface ViewController : UIViewController <UITextFieldDelegate>

然后当调用该代码时,您会在方法内部说
SKLabelNode = [NSString stringWithFormat:@"Thanks %@",textField.text]

于 2015-04-12T02:05:42.243 回答