18

enter image description hereUIPopover Come posso creare un popover con pulsanti come questo?

Mi chiedo come posso creare un popover con pulsanti come questo.

RISPOSTA:

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]; 

qualche altra parte nel vostro classe di oggetti delegato ...

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     // take photo... 
    } 
    else if (buttonIndex == 1) { 
     // choose existing photo... 
    } 
} 

risposta

43

Questo è un UIActionSheet. Su iPhone, si anima dal basso. Sull'iPad appare in un popover.

Supponendo che si sta facendo sulla pressione di un pulsante:

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]; 

In iOS 8 +, è necessario utilizzare il nuovo UIAlertController classe:

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]; 

o in Swift

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) 
+0

Devo semplicemente aggiungere questo alla vista Popover? – ManOx

+0

No, basta usare uno dei metodi showDrom di UIActionSheet .... Vedere la mia risposta aggiornata per un esempio –

+0

Ok e un'altra domanda, come posso impostare i gestori di eventi sui pulsanti? – ManOx

2

Simile alle altre risposte, ma questo è molto facile da implementare in confronto.

Fai in modo che la classe utilizzi lo UIActionSheetDelegate.

Esempio:

@interface ExampleViewController : UIViewController <UIActionSheetDelegate> 

Quindi aggiungere al vostro 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)"); } } 

Ora, in un evento premuto il pulsante o dove/quando si desidera che questo popup chiamare il seguente:

- (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]; 
} 

E ulteriori informazioni a questo proposito: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html

+1

Non dimenticare di aggiungere [actionSheet showFromRect: [(UIButton *) frame mittente] inView: self.view animated: YES]; per attaccare il popover al pulsante del mittente. –

Problemi correlati