Ескертпе
Бұл бетке кіру үшін қатынас шегін айқындау қажет. Жүйеге кіруді немесе каталогтарды өзгертуді байқап көруге болады.
Бұл бетке кіру үшін қатынас шегін айқындау қажет. Каталогтарды өзгертуді байқап көруге болады.
Начиная с iOS 8, UIAlertController завершил замену UIActionSheet и UIAlertView оба из которых устарели.
В отличие от замененных классов, которые являются подклассами UIView, UIAlertController является подклассом UIViewController.
Используется UIAlertControllerStyle для указания типа отображаемого оповещения. Эти типы оповещений:
- UIAlertControllerStyleActionSheet
- Предварительная iOS 8 это была бы UIActionSheet
- UIAlertControllerStyleAlert
- Предварительная версия iOS 8 была бы UIAlertView
При создании контроллера оповещений необходимо выполнить три необходимых действия.
Создайте и настройте оповещение с помощью:
- title
- message
- предпочтительный стиль
(Необязательно) Добавление текстового поля
Добавление обязательных действий
Представление контроллера представления
Самое простое оповещение содержит одну кнопку, как показано на этом снимке экрана:

Код для отображения простого оповещения выглядит следующим образом:
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);
};
Отображение оповещения с несколькими параметрами выполняется аналогично, но добавляет два действия. Например, на следующем снимок экрана показано оповещение с двумя кнопками:

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);
});
Оповещения также могут отображать лист действий, аналогичный снимку экрана ниже:

Кнопки добавляются в оповещение AddAction с помощью метода:
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);
});