Strange behavior of Cancel button's ActionSheet
I'm new in Objective-C, I try to code an iPad App (Objective-C 2.0, Xcode 4.0.1). I "play" with ActionSheet to understand how it works. In a nib file I put a button binded to my "buttonPressed" method.
I've written this code :
-(IBAction)buttonPressed
{
UIActionSheet *myActionSheet = [[UIActionSheet alloc] initWithTitle:@"My Action Sheet" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"OK"开发者_如何学编程 otherButtonTitles:@"Test", nil];
NSLog([NSString stringWithFormat:@"%d", [myActionSheet numberOfButtons]]);
NSLog([myActionSheet buttonTitleAtIndex:0]);
NSLog([myActionSheet buttonTitleAtIndex:1]);
NSLog([myActionSheet buttonTitleAtIndex:2]);
[myActionSheet showInView:self.view];
[myActionSheet release];
}
This is the NSLog result :
2011-04-14 14:52:30.428 TrampManage[3568:207] 3
2011-04-14 14:52:30.429 TrampManage[3568:207] OK
2011-04-14 14:52:30.430 TrampManage[3568:207] Test
2011-04-14 14:52:30.432 TrampManage[3568:207] Cancel
But the displayed view is quiet different (cf image : http://www.imagup.com/data/1117452257.html). For the moment, I've one simple question : Why my "Cancel" button doesn't appear ?
As your tag hints, you are probably working on an ipad project.
The documentation says about the cancel button for iPads:
cancelButtonTitle The title of the cancel button. This button is added to the action sheet automatically and assigned an appropriate index, which is available from the cancelButtonIndex property. This button is displayed in black to indicate that it represents the cancel action. Specify nil if you do not want a cancel button or are presenting the action sheet on an iPad.
You can cancel by touching outside the action sheet.
You're most likely on an iPad, where the new interface idiom is to not show the cancel button. A user can cancel the sheet by simply clicking outside the sheet. Not the best design decision IMHO, but that's the way it is now.
on ipad it doesn't shows the cancel button. because ipad has more width than the iphone & for dismiss it you can tab outside the actionsheet on ipad.
Detail explaination:
see this image of iphone which shows ok button
below Figure shows the action sheet when displayed on the iPad. Interestingly, on the iPad the OK button (set by the cancelButtonTitle: parameter) is not displayed.
The value (buttonIndex) of each button when clicked is as follows:
➤ Delete Message — 0
➤ Option 1 — 1
➤ Option 2 — 2
➤ OK — 3
On the iPad, when the user taps on an area outside of the action sheet, the action sheet is dismissed and the value of buttonIndex becomes 3. Interestingly, if you specified nil for the cancelButtonTitle: part, the value of buttonIndex would be –1 when the action sheet is dismissed.
精彩评论