Wednesday, December 12, 2012

Textfield on cocos2d Mac

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:


 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];
}

17 comments:

  1. Good ideas with professionals tips thank you so much!! looking for more such good ideas!!
    visit here

    ReplyDelete
  2. Wonderful blog! I truly love how it’s easy on my eyes as well as the info are well written. I am wondering how I may be notified whenever a new post has been made.
    Global education centre

    ReplyDelete
  3. Nice blog..! I really loved reading through this article. Thanks for sharing such a
    amazing post with us and keep blogging... best angularjs training institute in chennai | angularjs training in omr | angularjs best training center in chennai | angularjs training in chennai

    ReplyDelete
  4. Good blog information I have seened
    Sanjary Kids is one of the best play school and preschool in Hyderabad,India. Give your child the best preschool experience by choosing the best playschool of Hyderabad in Abids. we provide programs like Play group,Nursery,Junior KG,Senior KG,and provides Teacher Training Program.
    Preschool in hyderabad

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Nice blog post information it will useful and helpful

    Sanjary Academy is the best Piping Design institute in Hyderabad, Telangana. It is the best Piping design Course in India and we have offer professional Engineering Courses like Piping design Course, QA/QC Course, document controller course, Pressure Vessel Design Course, Welding Inspector Course, Quality Management Course and Safety Officer Course.
    Piping Design Course
    Piping Design Course in Hyderabad ­
    Piping Design Course in India­

    ReplyDelete
  8. Good blog information about the topic

    Sanjary Academy provide pressure vessel design, quality management system course piping design course, qa/qc course and document controller course.
    Welding Inspector Course
    Safety officer course
    Quality Management Course
    Quality Management Course in India

    ReplyDelete
  9. I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks
    Devi Ahilya Vishwa Vidhyalaya BCOM Time Table 2020

    ReplyDelete
  10. I think you did an awesome job explaining it. choi technique

    ReplyDelete
  11. what color lights help you sleep? The answer to this question is not as straightforward. However, white and red lights can make it harder for people who want some peaceful snoozing time by disrupting the natural circadian rhythm with blue tones instead!

    ReplyDelete
  12. Thanks for posting a very sweet article. So much needed data I'll get and keep in my file.Thank u so much.

    BCom 1st Year Admit Card 2022
    BSc 2nd Year Admit Card 2022
    BA 3rd Year Admit Card 2022

    ReplyDelete
  13. This blog is really interesting blog. It is so informative, if you are looking for best online reputation management tool then QuickMetrix is best option, It is an Integrated solution for Social Listening, Response Management, Social CRM, Social Analytics

    ReplyDelete
  14. nice article and keep it up!!

    Having some more related info SAP VC Training

    ReplyDelete
  15. Hi,
    I appreciate your kind words about the thought-provoking blog. It makes me happy to hear that you've received useful ideas, and I'm grateful for the opportunity to foster important interactions.
    Here issharing Enovia PLMinformation may be its helpful to you.
    Enovia PLM Training

    ReplyDelete