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
risposta
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.
Si tratta di un UIAlertController
su iOS 8+, e un UIActionSheet
sulle versioni precedenti.
È 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.
Per tutti coloro che sono alla ricerca di una soluzione a Swift:
Adottare
UIActionSheetDelegate
protocolloCreare 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)
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; } }
Creazione di un foglio di azione a Swift
Codice è stato aggiornato per Swift 3
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 cheUIActionSheet
.)
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. –
Funziona molto bene, ma possiamo personalizzare i pulsanti testo @Suragch –
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];
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;
}
}
- 1. Menu popup Bubble iOS simile a ITunes
- 2. menu popup iPhone come popover iPad?
- 3. creazione menu popup in Android 2.3 GingerBread
- 4. Menu di LinkedIn App per iPhone
- 5. Come creare menu popup
- 6. Menu popup personalizzato in VIM
- 7. Impossibile creare il menu "Condividi" nel menu popup su Android
- 8. espresso lungo clic sulla voce di menu e menu popup
- 9. Menu contestuale iPhone/iPad
- 10. finestra popup al menu di scelta rapida delle estensioni chrome '
- 11. Stile del menu popup in Android 5.0
- 12. Menu popup pop-up con margine
- 13. Menu popup auto-completo di Emacs interrotto
- 14. Come aggiungere elementi nel menu popup?
- 15. Colore testo menu popup android (AppCompat)
- 16. Problema del menu popup in Android ListView
- 17. Implementazione del menu popup da NSButton
- 18. Modifica stile editatext stile popup menu contestuale
- 19. Codice popup valutazione app iPhone
- 20. Come impostare il menu popup per la barra di intestazione di ListView insieme al menu di scelta rapida degli elementi?
- 21. come mappare le chiavi per il menu popup in vim
- 22. iphone menu laterale facebook usando Objective C
- 23. Chiusura Menu Contesto con Menu Menu Templati
- 24. Tirare per visualizzare il menu simile a NavBar nascosto in iOS? come visto in GetPocket App
- 25. ActionBarSherlock menu di scelta rapida colore del menu del menu
- 26. SplitView per ipad e menu nel cassetto per iPhone
- 27. Creazione di un menu di opzioni popup in una barra delle azioni contestuale
- 28. Menu attività Android Menu Problema
- 29. Menu a tendina personalizzati/menu a discesa
- 30. Creazione di un semplice menu multilivello CSS
migliore risorsa che ho trovato qui http://code.tutsplus.com/tutorials/uiactionsheet-and-uiactionsheetdelegate--mobile-11590 – Nepster