NSTextFiled value entery

Discussion in 'Mac Programming' started by larswik, Jun 27, 2011.

  1. larswik macrumors 68000

    Sep 8, 2006
    So I am close to finishing my new and improved dice rolling program, for gaming. I have a couple of questions to help me wrap it up.

    1) This code works when I enter a value into an NSTextField it then passes the value to an int variable
    setMinOpenEnderRange = [setOpenEndRangeValue intValue];
    But then I type in the the value and hit return the box is still highlighted like it wants to be typed in again?

    2) Jim helped me with this earlier and I get most of it except for the menuItem. I tried to read up on it today but I don't quite get it. I swapped out some of his code to fit mine.
        [self addSidedDiceSelection:100];
        [self addSidedDiceSelection:20];
        [self addSidedDiceSelection:10];
        [self addSidedDiceSelection:8];
        [self addSidedDiceSelection:6];
        [self addSidedDiceSelection:4];
        [self addSidedDiceSelection:2];
    - (void)addSidedDiceSelection:(int)numberOfSides
        NSNumber *side = [NSNumber numberWithInt: numberOfSides];
        NSString *title = [NSString stringWithFormat: @"d%@", side];
        [dieNumberPopUpDisply addItemWithTitle: title];
        NSMenuItem *menuItem = [sidedDiceSelection lastItem];
        [menuItem setRepresentedObject: side];
        //dieNumSelection = [menuItem];
        [menuItem setAction: @selector(selectDice:)];
        [menuItem setTarget: self];
    Everytime it runs through the iteration it get the value of the dice and assigns it to the NSNumber 'side'. Then an NSString 'title' is created from the number. The title is then added to my popUpButton. the rest of it I don't quite get?

    It runs fine and I can see the different titles in the popUpButton. What I don't understand is how I can retrieve the int value for the selection?

    So my code would be like
    dieRoll = arc4random() % diceSelection + 1; 
    How can I retrieve the value and assign it to my variable diceSelection? All of the rest of my code seems to work fine.

  2. mfram macrumors 65816

    Jan 23, 2010
    San Diego, CA USA
    You can retrieve which item has been selected in the popUp with:

    [dieNumberPopUpDisply indexOfSelectedItem]
    From that you can figure out which value to use in your program.
  3. jiminaus macrumors 65816


    Dec 16, 2010
    What do you want to happen?

    When you add an item to an NSPopUpButton, what it's doing for you is using the given title to create an NSMenuItem object with that same title and appending the new NSMenuItem object to it's NSMenu object (the one that pops up with it's clicked).

    After adding the item, the code goes on to get the newly added NSMenuItem object, which will be the last menu item.

    It then connects the new NSMenuItem object's action to the selectDice: outlet in the controller.

    In InterfaceBuilder, you connect an action to an outlet by dragging a connection from the source of the action to the target containing the outlet, you then go on to select which outlet of the target to actually connect to.

    If you do this in code, you do it similarly. Sending an the source a setTarget: message is sets the target object of the connection, this is equivalent to dragging from the source object to the target object.

    Next you send a setAction: message to select which outlet of the target will be called. This equivalent to selecting an outlet in InterfaceBuilder after dragging to the target.

    (Actually it doesn't matter about the order. You can send setTarget: then setAction:, or send setAction: and then setTarget:.)

    You may not seen the @selector(selectDice:) syntax before. I'll gloss of it by saving this is kind of a way to refer to a method without actually calling the method.

    Now it seems you may not actually need the code at all. If you don't want to react whenever the user popups up the menu and makes a selection, you don't to connect the menu items to an outlet. For example, if you just have a roll push button, then you can just get the currently select menu item in the outlet connected to that push button.

    I show you this code in your other post, but I'm not surprised it got lost. Sorry for bamboozling you with the awakeFromNib code.

    NSMenuItem *selectedMenuItem = [dieNumberPopUpDisply selectedItem];
    NSNumber *sidesObj = [selectedMenuItem representedObject];
    int sides = [sidesObj intValue];
    dieRoll = arc4random() % sides + 1;
  4. larswik thread starter macrumors 68000

    Sep 8, 2006
    Hi Jim, Thanks for the detailed reply, as usual :)

    The NSTextField works just fine. I enable it so the user can enter a number. That number is then put into an int variable. When I normally enter text into a text field box it highlights with a blinking cursor, enter the data and hot return. I know in this case return does nothing because when I press the 'Roll Dice' button I have a line of code that sets the data 'setMinOpenEnderRange = [setOpenEndRangeValue intValue];' Like I said the code works, it's just a little annoying looking at that blue highlighted text.

    It's after 2am in Santa Barbara and my eyes are going blurry. I will look over the popUp in the morning with fresh eyes.

    By the way, I saw this article on MacWorld website. It's about how Smart Phone developers are in high demand right now and the market is getting hotter. http://www.macworld.com/article/160780/2011/06/app_maker_demand.html I guess I picked a good year last year to start learning again.Next year I might be marketable :)



Share This Page