Getting access to the name of a button?

  • I've created a very simple application with just one button called "Button
    A" and I connected it to the action "myLog:" inside the AppController (I
    created the AppController by sub classing NSObject and I added the action
    myLog: using the IB. I then instantiated AppController and Control-dragged
    from the button to the AppController instance).
    The AppController has just one method that looks like this.

    - (IBAction)myLog:(id)sender
    {
        //NSLog(@"myLog activated");
        NSString *myString=[sender stringValue];
        NSLog(@"%@", myString);
    }

    When I run it and click repeatably on the button "Button A" this is what I
    get in the log window

    [Session started at 2007-09-14 12:19:23 +0200.]
    2007-09-14 12:19:29.660 Logger[482] 1
    2007-09-14 12:19:30.676 Logger[482] 0
    2007-09-14 12:19:31.708 Logger[482] 1
    2007-09-14 12:19:32.291 Logger[482] 0

    I was hoping that [sender stringValue] would return the name of the button,
    that is "Button A", but it gives alternating 1s and 0s. What's the
    explanation?

    Thanks Frank
  • NSButton uses NSButtonCell to implement its user interface.

    - (NSString *)stringValue  -- Returns the button value as a string

    To get Button A, you have to use - (NSString *)title method.

    On 14-Sep-07, at 3:58 PM, Frank Bettger wrote:

    > I've created a very simple application with just one button called
    > "Button
    > A" and I connected it to the action "myLog:" inside the
    > AppController (I
    > created the AppController by sub classing NSObject and I added the
    > action
    > myLog: using the IB. I then instantiated AppController and Control-
    > dragged
    > from the button to the AppController instance).
    > The AppController has just one method that looks like this.
    >
    > - (IBAction)myLog:(id)sender
    > {
    > //NSLog(@"myLog activated");
    > NSString *myString=[sender stringValue];
    > NSLog(@"%@", myString);
    > }
    >
    > When I run it and click repeatably on the button "Button A" this is
    > what I
    > get in the log window
    >
    > [Session started at 2007-09-14 12:19:23 +0200.]
    > 2007-09-14 12:19:29.660 Logger[482] 1
    > 2007-09-14 12:19:30.676 Logger[482] 0
    > 2007-09-14 12:19:31.708 Logger[482] 1
    > 2007-09-14 12:19:32.291 Logger[482] 0
    >
    > I was hoping that [sender stringValue] would return the name of the
    > button,
    > that is "Button A", but it gives alternating 1s and 0s. What's the
    > explanation?
    >
    > Thanks Frank
    >
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today