UIActionSheet question

Discussion in 'iPhone/iPad Programming' started by Jeremy1026, Sep 2, 2008.

  1. macrumors 68000

    Joined:
    Nov 3, 2007
    Messages:
    1,888
    #1
    Ok, I created my UIActionSheet and populated it with 5 buttons. Now how do I determine which button is pressed (to continue on with the operation of my application)?

    I tried using:
    Code:
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    	// the user clicked one of the OK/Cancel buttons
    	if (buttonIndex == 0)
    	{
    		NSLog(@"ok");
    	}
    }
    When I drop this into my application, NSLog doesn't show anything no matter what button I press?
     
  2. Moderator

    robbieduncan

    Staff Member

    Joined:
    Jul 24, 2002
    Messages:
    24,480
    Location:
    London
    #2
    Have you tried logging the buttonIndex to see what it gets set to? I'm sure the documentation tells you the values, but as I'm not on a Mac right now I can't tell you where.
     
  3. macrumors 6502a

    Joined:
    Nov 9, 2007
    Messages:
    706
    #3
    I seem to remember having this issue the other day when I used UIActionSheet for the first time - can't remember exactly what the issue was and I don't have my code nearby. However, it *might* be that you are trying to compare an NSNumber to an int. One thing that will work is to call [buttonIndex integerValue] and compare that to an int but there might be a more idiomatic way of doing this (look at the NSNumber documentation).
     
  4. Moderator

    robbieduncan

    Staff Member

    Joined:
    Jul 24, 2002
    Messages:
    24,480
    Location:
    London
    #4
    Can you pass messages to NSIntegers? I didn't think they were objects?
     
  5. macrumors 6502a

    Joined:
    Nov 9, 2007
    Messages:
    706
    #5
    You're right, they aren't. I misread the original code, I thought it was an NSNumber, which is a class with an integerValue method.
     
  6. Moderator

    robbieduncan

    Staff Member

    Joined:
    Jul 24, 2002
    Messages:
    24,480
    Location:
    London
    #6
    Having looked at the documentation it would appear that the first button should have index 0...
     
  7. macrumors 6502a

    Joined:
    Nov 9, 2007
    Messages:
    706
    #7
    Had a look at my own UIActionSheet delegate method and I'm using a switch statement:

    Code:
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
      switch (buttonIndex) {
        case 0:
           // do something
          break;
        case 1: 
           // do something else
        default:
          break;
      }
    
     

Share This Page