Wyświetlanie alertów na platformie Xamarin.iOS
Począwszy od systemu iOS 8, interfejs użytkownikaAlertController został ukończony zastąpiony UIActionSheet i UIAlertView, które są teraz przestarzałe.
W przeciwieństwie do klas, które zostały zastąpione, które są podklasami UIView, UIAlertController jest podklasą UIViewController.
Użyj UIAlertControllerStyle
polecenia , aby wskazać typ alertu do wyświetlenia. Te typy alertów to:
- UIAlertControllerStyleActionSheet
- Wstępnie dla systemu iOS 8 byłby to arkusz UIActionSheet
- UIAlertControllerStyleAlert
- W wersji wstępnej dla systemu iOS 8 byłaby to funkcja UIAlertView
Podczas tworzenia kontrolera alertów należy wykonać trzy kroki:
Utwórz i skonfiguruj alert za pomocą polecenia :
- title
- wiadomość
- preferredStyle
(Opcjonalnie) Dodawanie pola tekstowego
Dodawanie wymaganych akcji
Prezentowanie kontrolera widoku
Najprostszy alert zawiera jeden przycisk, jak pokazano na poniższym zrzucie ekranu:
Kod do wyświetlenia prostego alertu jest następujący:
okayButton.TouchUpInside += (sender, e) => {
//Create Alert
var okAlertController = UIAlertController.Create ("Title", "The message", UIAlertControllerStyle.Alert);
//Add Action
okAlertController.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));
// Present Alert
PresentViewController (okAlertController, true, null);
};
Wyświetlanie alertu z wieloma opcjami odbywa się w podobny sposób, ale dodaje dwie akcje. Na przykład poniższy zrzut ekranu przedstawia alert z dwoma przyciskami:
okayCancelButton.TouchUpInside += ((sender, e) => {
//Create Alert
var okCancelAlertController = UIAlertController.Create("Alert Title", "Choose from two buttons", UIAlertControllerStyle.Alert);
//Add Actions
okCancelAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, alert => Console.WriteLine ("Okay was clicked")));
okCancelAlertController.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, alert => Console.WriteLine ("Cancel was clicked")));
//Present Alert
PresentViewController(okCancelAlertController, true, null);
});
Alerty mogą również wyświetlać arkusz akcji podobny do poniższego zrzutu ekranu:
Przyciski są dodawane do alertu za pomocą AddAction
metody :
actionSheetButton.TouchUpInside += ((sender, e) => {
// Create a new Alert Controller
UIAlertController actionSheetAlert = UIAlertController.Create("Action Sheet", "Select an item from below", UIAlertControllerStyle.ActionSheet);
// Add Actions
actionSheetAlert.AddAction(UIAlertAction.Create("OK",UIAlertActionStyle.Default, (action) => Console.WriteLine ("Item One pressed.")));
actionSheetAlert.AddAction(UIAlertAction.Create("custom button 1",UIAlertActionStyle.Default, (action) => Console.WriteLine ("Item Two pressed.")));
actionSheetAlert.AddAction(UIAlertAction.Create("Cancel",UIAlertActionStyle.Cancel, (action) => Console.WriteLine ("Cancel button pressed.")));
// Required for iPad - You must specify a source for the Action Sheet since it is
// displayed as a popover
UIPopoverPresentationController presentationPopover = actionSheetAlert.PopoverPresentationController;
if (presentationPopover!=null) {
presentationPopover.SourceView = this.View;
presentationPopover.PermittedArrowDirections = UIPopoverArrowDirection.Up;
}
// Display the alert
this.PresentViewController(actionSheetAlert,true,null);
});