dotnet/maui · error · InvalidOperationException

No UIViewController found to present.

Error message

No UIViewController found to present.

What it means

ContextActionCell throws InvalidOperationException("No UIViewController found to present.") when its controller-walk returns null. iOS requires a UIViewController to present a UIAlertController for context actions; if the cell is not attached to any window/view hierarchy, no controller can be located and the action sheet cannot be shown.

Source

Thrown at src/Compatibility/Core/src/iOS/ContextActionCell.cs:340

					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. Only trigger context actions after the cell's ListView is visible in a windowed UIViewController.
  2. Cancel pending context actions on cell recycling/detachment.
  3. In tests/hosted scenarios, ensure the page is presented before invoking context action menu items.

Example fix

// before
((IMenuItemController)menuItem).Activate();
// after
if (GetController(cell) != null)
    ((IMenuItemController)menuItem).Activate();
Defensive patterns

Strategy: validation

Validate before calling

// Ensure a controller exists before activating context actions.
var controller = FindViewController(cell);
if (controller == null) return; // not yet presented

Type guard

static bool IsCellPresented(UITableViewCell cell) =>
    cell.Window != null && FindViewController(cell) != null;

Prevention

When it happens

Trigger: Activating context actions on a cell whose ViewCell/ListView is not yet in a windowed UIViewController hierarchy; calling Activate on a MenuItem for a cell that was detached, recycled, or never displayed.

Common situations: Programmatically triggering a MenuItem.Activate() outside the live UI; cells recycled while a context action is still pending; modal transitions detaching the view; tests invoking context actions without a hosted controller.

Related errors


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