2013-06-20 10 views
58

Vorrei creare questo tipo di menu, ovviamente con altri pulsanti di menu. Esiste un viewcontroller predefinito che lo rappresenta, oppure devo ottenere immagini e crearlo da solo.Creazione di UIActionSheet

enter image description here

+3

Il titolo e la popolarità di questa domanda è molto meglio del duplicato. Sarebbe bello riaprire questa domanda in modo da poter aggiungere risposte aggiornate. – Suragch

+2

Vota per riaprire per favore. – Stunner

+0

@ Suragch hai ragione. è più utile e comprensibile di altri link –

risposta

11

Date un'occhiata alla documentazione di UIActionSheet.

NSString *actionSheetTitle = @"Action Sheet Demo"; //Action Sheet Title 
NSString *destructiveTitle = @"Destructive Button"; //Action Sheet Button Titles 
NSString *other1 = @"Other Button 1"; 
NSString *other2 = @"Other Button 2"; 
NSString *other3 = @"Other Button 3"; 
NSString *cancelTitle = @"Cancel Button"; 
UIActionSheet *actionSheet = [[UIActionSheet alloc] 
           initWithTitle:actionSheetTitle 
           delegate:self 
           cancelButtonTitle:cancelTitle 
           destructiveButtonTitle:destructiveTitle 
           otherButtonTitles:other1, other2, other3, nil]; 
[actionSheet showInView:self.view]; 
169

È necessario utilizzare un UIActionSheet.

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

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 deprecato per iOS 8.x https://developer.apple.com/documentation/uikit/uialertcontroller#//apple_ref/occ/cl/UIAlertController

+5

+! per essere l'unico a chiamarlo "UIActionSheet" (oltre a fornire una buona risposta). – rmaddy

+1

Stavo letteralmente lavorando alla mia app mentre vedevo la domanda! Viene dal mio codice verbatim;) – ApolloSoftware

+1

funziona anche senza aggiungere UIActionSheetDelegate .. Voglio sapere come ... – Nepster

5

Si chiama un UIActionSheet: Si crea uno in questo modo:

NSString *actionSheetTitle = @"Action Sheet Demo"; //Action Sheet Title 
NSString *destructiveTitle = @"Destructive Button"; //Action Sheet Button Titles 
NSString *other1 = @"Other Button 1"; 
NSString *other2 = @"Other Button 2"; 
NSString *other3 = @"Other Button 3"; 
NSString *cancelTitle = @"Cancel Button"; 
UIActionSheet *actionSheet = [[UIActionSheet alloc] 
           initWithTitle:actionSheetTitle 
           delegate:self 
           cancelButtonTitle:cancelTitle 
           destructiveButtonTitle:destructiveTitle 
           otherButtonTitles:other1, other2, other3, nil]; 
[actionSheet showInView:self.view]; 

Implementare l'UISctionSheetDelegate di rispondere all'azione pulsante.

Date un'occhiata a questo tutorial per maggiori informazioni: http://mobile.tutsplus.com/tutorials/iphone/uiactionsheet_uiactionsheetdelegate (Codice è da questo tutorial)

Problemi correlati