Crash: Focus item <UITableViewCell> does not provide a parentFocusEnvironment.

I'm on Mac Catalyst.

I have a UISearchController and I keep getting this crash after I clear the search bar, adjust the search scope segmented control if one of the UITableViewCells is focused (via arrow key press) before

Focus item does not provide a parentFocusEnvironment.

I can workaround the problem by overriding -parentFocusEnvironment, holding the parent UITableView in a property and returning it:

-(id<UIFocusEnvironment>)parentFocusEnvironment
{
     id<UIFocusEnvironment>theFocus = [super parentFocusEnvironment];

    if (theFocus == nil)
   {
        return self.cachedParentFocus;
   }
   else 
  {
     self.cachedParentFocus = theFocus
  }

    return theFocus;
}

The problem with this is it is likely to create a retain cycle (I did try a weak reference but that can cause the following crasher on deallocation (the focus environment continues to call this method on the table view cell even when its outside of a UIWindow):

“Cannot form weak reference to instance (0x13799a000) of class UITableView. It is possible that this object was over-released, or is in the process of deallocation.”

Anyone run into this and know of a potential workaround?

So I'm going with the workaround above (for now at least). You have to be careful to ensure that you only set the fallback weak reference once (I'm doing it in -didAddSubview: from a UITableView subclass) and don't accidentally set it while the tableview is in the process of being torn down.

Attempting to set it from within -parentFocusEnvironment can cause the weak reference crash since the focus system is very aggressive and it interrogates the focus environment during/after it has been removed from the window.

Crash: Focus item &lt;UITableViewCell&gt; does not provide a parentFocusEnvironment.
 
 
Q