问题
In Apple's stock 'Messages' app, tapping the camera button reveals popup buttons allowing the user to take a photo/video or choose an existing one. How would I implemented this same button design? Is the procedure the same for both iPhone & iPad?
回答1:
It's called a UIActionSheet. You use it like this
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"Foo" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"foo1", @"foo2", nil];
[action showInView:self.view];
(change the foos to whatever). To detect what button was clicked, implement the UIActionSheetDelegate's actionSheet:clickedButtonAtIndex: delegate method. For example:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:@"foo1"]) {
// do stuff...
}
}
And yes, this works on both the iPhone and iPad (as @bobnoble pointed out, the iPad version uses a popover view, not an action sheet, but action sheets work on both).
来源:https://stackoverflow.com/questions/17374212/creating-a-pop-up-button-interface