Surprisingly adding a textfield in cocos2d Mac isn't as simple as in cocos2d iOS. There are various suggestions by other coders on how to do this.
This sample code tells you one of the several ways to deal with the textfield in cocos2d Mac.
HERE is the download link.
In this sample code, I have created an external window which opens up when the user hits the textfield.
Here is the UserInterface of nib file. (TextFieldView.xib).
We have following connections on this window.
When I run the app, my window looks like following with textfield which is actually a touch responsive CCMenuItemImage and on top of it sits a CCLabelTTF which says Enter UserName. This string"Enter UserName" will be changed to whatever user enters in the Textfield popup.
When I click on the Enter Username with my mouse, it opens the popup with the textfield. Following code is executed to make the window pop up:
We also need to define a selector which is where the control comes back when the popup is gone, hence we implement the following:
When you are done entering the value in textfield popup, just hit the OK button. When you do that following method(implemented in TextFieldView.m ) is executed. This method makes the popup go away. This is the selector method attached with OK button. You can view the connections in the Xib Interface Window in Xcode (already described above).
This sample code tells you one of the several ways to deal with the textfield in cocos2d Mac.
HERE is the download link.
In this sample code, I have created an external window which opens up when the user hits the textfield.
Here is the UserInterface of nib file. (TextFieldView.xib).
We have following connections on this window.
When I run the app, my window looks like following with textfield which is actually a touch responsive CCMenuItemImage and on top of it sits a CCLabelTTF which says Enter UserName. This string"Enter UserName" will be changed to whatever user enters in the Textfield popup.
When I click on the Enter Username with my mouse, it opens the popup with the textfield. Following code is executed to make the window pop up:
TextfieldProjectAppDelegate* appDelegate = (TextfieldProjectAppDelegate*)[[NSApplication sharedApplication] delegate];
tView = [[TextFieldView alloc] initWithWindowNibName:@"TextFieldView"];
assert ([appDelegate window]);
assert ([tView window]);
[NSApp beginSheet:[tView window]
modalForWindow:[appDelegate window]
modalDelegate:self
didEndSelector:@selector(usernameSheetDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
We also need to define a selector which is where the control comes back when the popup is gone, hence we implement the following:
- (void)usernameSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
[sheet orderOut:nil];
NSLog(@"name == %@", tView.username);
//Need to update text on textfield
CCLabelTTF *userName=(CCLabelTTF*)[self getChildByTag:106];
userName.string=tView.username;
}
When you are done entering the value in textfield popup, just hit the OK button. When you do that following method(implemented in TextFieldView.m ) is executed. This method makes the popup go away. This is the selector method attached with OK button. You can view the connections in the Xib Interface Window in Xcode (already described above).
- (IBAction)usernameEntered:(id)sender {
username=[nameField stringValue];
[NSApp endSheet:self.window];
}