我想知道我怎么可以创建这样的按钮的酥料饼。
回答:
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil
delegate: self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles: @"Take Photo",
@"Choose Existing Photo", nil];
[actionSheet showFromRect: button.frame inView: button.superview animated: YES];
别的地方在你的授权对象类...
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
// take photo...
}
else if (buttonIndex == 1) {
// choose existing photo...
}
}
这是一个UIActionSheet
。 在iPhone上,它从底部动画英寸 在iPad上它出现在一个酥料饼。
假设你在按一下按钮,这样做:
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil
delegate: self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles: @"Take Photo",
@"Choose Existing Photo", nil];
[actionSheet showFromRect: button.frame inView: button.superview animated: YES];
在iOS8上的+,您应该使用新的UIAlertController
类:
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil
message: nil
preferredStyle: UIAlertControllerStyleActionSheet];
[alertController addAction: [UIAlertAction actionWithTitle: @"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Handle Take Photo here
}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Handle Choose Existing Photo here
}]];
alertController.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController * popover = alertController.popoverPresentationController;
popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
popover.sourceView = sender;
popover.sourceRect = sender.bounds;
[self presentViewController: alertController animated: YES completion: nil];
或者斯威夫特
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
alertController.addAction(UIAlertAction(title: "Take Photo", style: .Default, handler: { alertAction in
// Handle Take Photo here
}))
alertController.addAction(UIAlertAction(title: "Choose Existing Photo", style: .Default, handler: { alertAction in
// Handle Choose Existing Photo
}))
alertController.modalPresentationStyle = .Popover
let popover = alertController.popoverPresentationController!
popover.permittedArrowDirections = .Up
popover.sourceView = sender
popover.sourceRect = sender.bounds
presentViewController(alertController, animated: true, completion: nil)
类似于其他反应,但这样很容易比较来实现。
让你的类使用UIActionSheetDelegate。
例:
@interface ExampleViewController : UIViewController <UIActionSheetDelegate>
然后添加到您的ExampleViewController.mm/m
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { //Get the name of the current pressed button
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"Remove"]) {
NSLog(@"Remove this actionSheet"); }
if ([buttonTitle isEqualToString:@"Button 1"]) {
NSLog(@"Button 1 pressed"); }
if ([buttonTitle isEqualToString:@"Button 2"]) {
NSLog(@"Button 2 pressed"); }
if ([buttonTitle isEqualToString:@"Button 3"]) {
NSLog(@"Button 3 pressed"); }
if ([buttonTitle isEqualToString:@"Cancel"]) {
NSLog(@"Cancel clicked (anywhere away from it)"); } }
现在,在按下的按钮事件或者/当你想这个弹出拨打以下几点:
- (IBAction)aButtonPressed:(id)sender {
NSString *actionSheetTitle = @"Action Sheet"; // Title
NSString *destroyTitle = @"Destroy"; // Button titles
NSString *button1 = @"Button 1";
NSString *button2 = @"Button 2";
NSString *button3 = @"Button 3";
NSString *cancelTitle = @"Cancel";
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:actionSheetTitle
delegate:self
cancelButtonTitle:cancelTitle
destructiveButtonTitle:destroyTitle
otherButtonTitles:button1, button2, button3, nil]; [actionSheet showInView:self.view];
}
而关于这个@的更多信息: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html