2009-05-26 17 views
18

Vorrei creare un menu a comparsa simile a quello trovato nell'app di posta quando si desidera rispondere a un messaggio. Ho visto questo in più di un'applicazione quindi non ero sicuro se ci fosse qualcosa nel framework per esso o qualche codice di esempio.Creazione menu popup per iPhone simile al menu app Mail

UIActionSheet example

+0

migliore risorsa che ho trovato qui http://code.tutsplus.com/tutorials/uiactionsheet-and-uiactionsheetdelegate--mobile-11590 – Nepster

risposta

14

Controlla l'esempio UICatalog sul sito Web di Apple. La sezione "Avvisi" contiene esempi su come utilizzare UIActionSheet per realizzare ciò che stai cercando di fare.

9

È necessario utilizzare un UIActionSheet.

Per prima cosa è necessario aggiungere UIActionSheetDelegate al file .C ViewController.

Quindi è possibile fare riferimento a un actionsheet con:

UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Select Sharing option:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles: 
         @"Share on Facebook", 
         @"Share on Twitter", 
         @"Share via E-mail", 
         @"Save to Camera Roll", 
         @"Rate this App", 
         nil]; 
    popup.tag = 1; 
    [popup showInView:self.view]; 

poi si deve gestire ciascuna delle chiamate.

- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex { 

    switch (popup.tag) { 
    case 1: { 
     switch (buttonIndex) { 
      case 0: 
       [self FBShare]; 
       break; 
      case 1: 
       [self TwitterShare]; 
       break; 
      case 2: 
       [self emailContent]; 
       break; 
      case 3: 
       [self saveContent]; 
       break; 
      case 4: 
       [self rateAppYes]; 
       break; 
      default: 
       break; 
     } 
     break; 
    } 
    default: 
     break; 
} 
} 

Questo è stato dichiarato obsoleto a partire da iOS 8.x.

https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIAlertController_class/index.html

2

Per tutti coloro che sono alla ricerca di una soluzione a Swift:

  1. Adottare UIActionSheetDelegate protocollo

  2. Creare e mostrare l'ActinSheet:

    let sheet: UIActionSheet = UIActionSheet() 
    
    sheet.addButtonWithTitle("button 1") 
    sheet.addButtonWithTitle("button 2") 
    sheet.addButtonWithTitle("button 3") 
    sheet.addButtonWithTitle("Cancel") 
    sheet.cancelButtonIndex = sheet.numberOfButtons - 1 
    sheet.delegate = self 
    sheet.showInView(self.view) 
    
  3. Il delegate functi on:

    func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){ 
    switch buttonIndex{ 
        case 0: 
         NSLog("button1"); 
        case 1: 
         NSLog("button2"); 
        case 2: 
         NSLog("button3"); 
        case actionSheet.cancelButtonIndex: 
         NSLog("cancel"); 
         break; 
        default: 
         NSLog("blub"); 
         break; 
        } 
    } 
    
19

Creazione di un foglio di azione a Swift

Codice è stato aggiornato per Swift 3

enter image description here

Dal iOS 8, UIAlertController combinato con UIAlertControllerStyle.ActionSheet viene utilizzato . UIActionSheet è deprecato.

Ecco il codice per produrre il foglio di azione nell'immagine sopra:

class ViewController: UIViewController { 

    @IBOutlet weak var showActionSheetButton: UIButton! 

    @IBAction func showActionSheetButtonTapped(sender: UIButton) { 

     // Create the action sheet 
     let myActionSheet = UIAlertController(title: "Color", message: "What color would you like?", preferredStyle: UIAlertControllerStyle.actionSheet) 

     // blue action button 
     let blueAction = UIAlertAction(title: "Blue", style: UIAlertActionStyle.default) { (action) in 
      print("Blue action button tapped") 
     } 

     // red action button 
     let redAction = UIAlertAction(title: "Red", style: UIAlertActionStyle.default) { (action) in 
      print("Red action button tapped") 
     } 

     // yellow action button 
     let yellowAction = UIAlertAction(title: "Yellow", style: UIAlertActionStyle.default) { (action) in 
      print("Yellow action button tapped") 
     } 

     // cancel action button 
     let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (action) in 
      print("Cancel action button tapped") 
     } 

     // add action buttons to action sheet 
     myActionSheet.addAction(blueAction) 
     myActionSheet.addAction(redAction) 
     myActionSheet.addAction(yellowAction) 
     myActionSheet.addAction(cancelAction) 

     // support iPads (popover view) 
     myActionSheet.popoverPresentationController?.sourceView = self.showActionSheetButton 
     myActionSheet.popoverPresentationController?.sourceRect = self.showActionSheetButton.bounds 

     // present the action sheet 
     self.present(myActionSheet, animated: true, completion: nil) 
    } 
} 

ancora bisogno di aiuto? Guarda questo video tutorial. È così che l'ho imparato.

  • UIActionSheet example in Swift (Contrariamente al nome, in realtà fa utilizzare il nuovo foglio UIAlertController azione piuttosto che UIActionSheet.)
+0

Ciao @Suragch, provo il tuo codice. Funziona per me in iPad e iPod. Tuttavia, il pulsante Annulla non viene visualizzato in iPad. Non so perché. Mostra in iPod e io non uso il pulsante. Io uso l'immagine e aggiungo la funzione tocco su di esso. Quindi, il mio codice è alertController.popoverPresentationController? .sourceView = self.imgPlay e alertController.popoverPresentationController? .sourceRect = self.imgPlay.bounds. –

+0

Funziona molto bene, ma possiamo personalizzare i pulsanti testo @Suragch –

5

Questo è come si farebbe in Objective-C su iOS 8+:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Directions" 
                      message:@"Select mode of transportation:" 
                    preferredStyle:UIAlertControllerStyleActionSheet]; 
    UIAlertAction *drivingAction = [UIAlertAction actionWithTitle:@"Driving" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 
     // this block runs when the driving option is selected 
    }]; 
    UIAlertAction *walkingAction = [UIAlertAction actionWithTitle:@"Walking" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 
     // this block runs when the walking option is selected 
    }]; 
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; 
    [alert addAction:drivingAction]; 
    [alert addAction:walkingAction]; 
    [alert addAction:defaultAction]; 
    [self presentViewController:alert animated:YES completion:nil]; 
0

Ho provato ad aggiungere ActionSheet sul mio punto di vista. Quindi ho cercato di trovare la soluzione perfetta ma alcune risposte mi hanno confuso. Perché la maggior parte delle domande su Action sheet sono state scritte tanto tempo fa. Inoltre non è stato aggiornato. Ad ogni modo ... Scriverò la versione precedente di ActionSheet e la versione aggiornata di ActionSheet. Spero che la mia risposta sia in grado di rendere sereno il tuo cervello.

---------- --------- versione aggiornata

UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"Action Sheet" message:@"writeMessageOrsetAsNil" preferredStyle:UIAlertControllerStyleActionSheet]; 

    UIAlertAction* actionSheet01 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
                   handler:^(UIAlertAction * action) { NSLog(@"OK click");}]; 

    UIAlertAction* actionSheet02 = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault 
                   handler:^(UIAlertAction * action) {NSLog(@"OK click");}]; 

    UIAlertAction* actionSheet03 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel 
                   handler:^(UIAlertAction * action) { 
                    NSLog(@"Cancel click");}]; 

    [browserAlertController addAction:actionSheet01]; 
    [browserAlertController addAction:actionSheet02]; 
    [browserAlertController addAction:actionSheet03]; 

    [self presentViewController:browserAlertController animated:YES completion:nil]; 

------- ------ vecchia versione

UIActionSheet *actionSheet= [[UIActionSheet alloc] initWithTitle:@"Select Sharing option:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@“OK”, @“NO”,@“Cancel”, 
         nil]; 
    actionSheet.tag = 100; 
    [actionSheet showInView:self.view]; 

- (void)actionSheet:(UIActionSheet *)actionShee clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if(actionSheet.tag == 100){ 
     switch (buttonIndex) { 
      case 0: 
       [self doSomething]; 
       break; 
      case 1: 
       [self doAnything]; 
       break; 
      case 2: 
       [self doNothing]; 
       break; 
      default: 
       break; 
     } 
     break; 
    } 

} 
Problemi correlati