1

I am using Objective-C to make an iOS SpriteKit "GameScene" file. I have an SKLabelNode and a UITextField to input a player's name.

How can I make whatever the UITextField value equals to change the SKLabelNode to say "Thanks," + UITextField value?

(my code) :

#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
Cody Taylor
  • 31
  • 1
  • 8

1 Answers1

1

You need to use a delegate method. Specifically

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

to do this when you create your textFeild add this line of code

[textfield setDelegate:self]

also to add in that you fallow the delegate in you .h file

@interface ViewController : UIViewController <UITextFieldDelegate>

then when that code gets called you would say inside the method
SKLabelNode = [NSString stringWithFormat:@"Thanks %@",textField.text]

Charlie
  • 222
  • 3
  • 20
  • [textfield setDelegate:self] shows error : Expecting Method Body – Cody Taylor Apr 12 '15 at 02:16
  • mhmmmm. With just that little information I have no idea what would cause that. Did you also add the 3rd piece of code to the .h file. When you added it it should have been @interface (whatever was there before): (what ever was there before) last part is the only thing you should have added – Charlie Apr 12 '15 at 02:25
  • Sorry, Charlie. I appreciate the answer regarding UITextField action, but decided to reprogram the GameScene file. Thanks so much for sharing your knowledge for iOS dev! – Cody Taylor Apr 12 '15 at 02:47
  • It works, so long as all 3 previously written rules are followed! Especially @interface ViewController : UIViewController is done. REMEMBER PEOPLE, place INSIDE THE.h FILE!!! NOT THE .m FILE! Also, make sure your SKScene .m file has as well! – Krekin Apr 06 '16 at 14:30