PDA

View Full Version : UIActionSheet question




Jeremy1026
Sep 2, 2008, 09:21 AM
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:
- (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?



robbieduncan
Sep 2, 2008, 09:42 AM
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.

Luke Redpath
Sep 2, 2008, 10:20 AM
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).

robbieduncan
Sep 2, 2008, 10:43 AM
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).

Can you pass messages to NSIntegers? I didn't think they were objects?

Luke Redpath
Sep 2, 2008, 11:47 AM
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.

robbieduncan
Sep 2, 2008, 12:17 PM
Having looked at the documentation it would appear that the first button should have index 0...

Luke Redpath
Sep 2, 2008, 12:49 PM
Had a look at my own UIActionSheet delegate method and I'm using a switch statement:


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