dotnet/maui · error · InvalidOperationException

No UIViewController found to present.

Error message

No UIViewController found to present.

What it means

Thrown by the iOS ContextActionCell when GetController() returns null, meaning no UIViewController could be located up the view hierarchy to present the action sheet for context actions. On iPhone/iPad the action sheet (UIAlertController) requires a presenting view controller.

Source

Thrown at src/Controls/src/Core/Compatibility/Handlers/ListView/iOS/ContextActionCell.cs:349

					continue;

				var item = _cell.ContextActions[i];
				var weakItem = new WeakReference<MenuItem>(item);
				var action = UIAlertAction.Create(item.Text, UIAlertActionStyle.Default, a =>
				{
					if (_scroller == null)
						return;

					_scroller.SetContentOffset(new PointF(0, 0), true);
					if (weakItem.TryGetTarget(out MenuItem mi))
						((IMenuItemController)mi).Activate();
				});
				actionSheet.AddAction(action);
			}

			var controller = GetController();
			if (controller == null)
				throw new InvalidOperationException("No UIViewController found to present.");

			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
			{
				if (actionSheet.PopoverPresentationController != null)
				{
					actionSheet.PopoverPresentationController.SourceView = _tableView;
					actionSheet.PopoverPresentationController.SourceRect = sourceRect;
				}
			}

			controller.PresentViewController(actionSheet, true, null);
		}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Ensure context actions are only user-triggered after the page is fully presented and on screen.
  2. Delay activation until UIApplication.SharedApplication.KeyWindow.RootViewController is available.
  3. Avoid invoking context actions during lifecycle transitions (appearing/disappearing).

Example fix

// before
await Task.Run(() => menuItem.Activate()); // cell not yet on screen

// after
await Page.Appeared;
((IMenuItemController)menuItem).Activate();
Defensive patterns

Strategy: validation

Validate before calling

// Only activate context actions once the cell is attached to a window
if (GetController() is null) return; // not yet presentable

Try / catch

try { ((IMenuItemController)menuItem).Activate(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("No UIViewController"))
{ /* defer until the page is presented */ }

Prevention

When it happens

Trigger: Triggering context actions on a cell whose view is not currently attached to a window/view-controller hierarchy (e.g. measured off-screen, in a detached window, or during teardown). Presenting context actions before the view is on screen. Window/hierarchy not yet established at activation time.

Common situations: Activating context actions during view recycling or before the page is fully presented. Cells inside a popover/secondary window whose controller chain is incomplete. Programmatic activation of a menu item before navigation completes.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/bbf24c645968b76e. Report an issue: GitHub.