From 7a4208336df8f65f297d60ab84940ef4eac06f31 Mon Sep 17 00:00:00 2001 From: Vitaly Knyazev Date: Mon, 11 Mar 2024 13:01:50 +0000 Subject: [PATCH] Fixed issue 15874 - NRE when iOS app is running on MacOS and "More" item is clicked in ListView context menu --- Xamarin.Forms.Platform.iOS/ContextActionCell.cs | 9 ++++++--- Xamarin.Forms.Platform.iOS/Platform.cs | 15 +++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Xamarin.Forms.Platform.iOS/ContextActionCell.cs b/Xamarin.Forms.Platform.iOS/ContextActionCell.cs index 67fb799b4f9..7df1d2ebb47 100644 --- a/Xamarin.Forms.Platform.iOS/ContextActionCell.cs +++ b/Xamarin.Forms.Platform.iOS/ContextActionCell.cs @@ -358,15 +358,18 @@ void ActivateMore() if (controller == null) throw new InvalidOperationException("No UIViewController found to present."); - if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone) + if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone || (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad && actionSheet.PopoverPresentationController == null)) { var cancel = UIAlertAction.Create(StringResources.Cancel, UIAlertActionStyle.Cancel, null); actionSheet.AddAction(cancel); } else { - actionSheet.PopoverPresentationController.SourceView = _tableView; - actionSheet.PopoverPresentationController.SourceRect = sourceRect; + if (actionSheet.PopoverPresentationController != null) + { + actionSheet.PopoverPresentationController.SourceView = _tableView; + actionSheet.PopoverPresentationController.SourceRect = sourceRect; + } } controller.PresentViewController(actionSheet, true, null); diff --git a/Xamarin.Forms.Platform.iOS/Platform.cs b/Xamarin.Forms.Platform.iOS/Platform.cs index 35d70d8c931..00b68f88b48 100644 --- a/Xamarin.Forms.Platform.iOS/Platform.cs +++ b/Xamarin.Forms.Platform.iOS/Platform.cs @@ -472,7 +472,11 @@ static void PresentPopUp(UIWindow window, UIAlertController alert, ActionSheetAr { UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications(); var observer = NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification, - n => { alert.PopoverPresentationController.SourceRect = window.RootViewController.View.Bounds; }); + n => + { + if (alert.PopoverPresentationController != null) + alert.PopoverPresentationController.SourceRect = window.RootViewController.View.Bounds; + }); arguments.Result.Task.ContinueWith(t => { @@ -480,9 +484,12 @@ static void PresentPopUp(UIWindow window, UIAlertController alert, ActionSheetAr UIDevice.CurrentDevice.EndGeneratingDeviceOrientationNotifications(); }, TaskScheduler.FromCurrentSynchronizationContext()); - alert.PopoverPresentationController.SourceView = window.RootViewController.View; - alert.PopoverPresentationController.SourceRect = window.RootViewController.View.Bounds; - alert.PopoverPresentationController.PermittedArrowDirections = 0; // No arrow + if (alert.PopoverPresentationController != null) + { + alert.PopoverPresentationController.SourceView = window.RootViewController.View; + alert.PopoverPresentationController.SourceRect = window.RootViewController.View.Bounds; + alert.PopoverPresentationController.PermittedArrowDirections = 0; // No arrow + } } if(!Forms.IsiOS9OrNewer)