iOS でアクションシート表示用のクラス UIActionSheet が iOS8 から deprecated になるため,
代わりに UIAlertController を使うことが推奨されています.
しかしこの UIAlertController ですが, これは PopoverPresentationController の指定をしていないと iPad での閲覧時に落ちます. この指定についてメモです. (Obj-C や Swift の記事はたくさんあるんですけど, C# (Xamarin) のものはあまり見かけないので, 書きました.)
書いたコード (C#)
// UIAlertController は iOS8 以降.
if (UIDevice.CurrentDevice.CheckSystemVersion(major: 8, minor: 0))
{
var alert = UIAlertController.Create(
title: null
, message: null
, preferredStyle: UIAlertControllerStyle.ActionSheet
);
alert.AddAction(UIAlertAction
.Create(
title: "何か処理名"
, style: UIAlertActionStyle.Default
, handler: a => {
//処理
})
);
alert.AddAction(UIAlertAction
.Create(
title: "キャンセル"
, style: UIAlertActionStyle.Cancel
, handler: null)
);
// if iPad
if (alert.PopoverPresentationController != null)
{
alert.PopoverPresentationController
.SourceView = this.View;
alert.PopoverPresentationController
.PermittedArrowDirections = UIPopoverArrowDirection.Up;
alert.PopoverPresentationController
.SourceRect = /* よしなに指定 */; // PushButton みたいなイベントで座標取得したほうが良いと思う
}
PresentViewController(
viewControllerToPresent: alert
, animated: false
, completionHandler: null
);
}
// iOS7以下
else
{
var alert = new UIActionSheet(
... 略
}