@@ -4,7 +4,7 @@ import { ViewBase } from "../view-base";
44
55import {
66 ViewCommon , layout , isEnabledProperty , originXProperty , originYProperty , automationTextProperty , isUserInteractionEnabledProperty ,
7- traceEnabled , traceWrite , traceCategories , traceMessageType
7+ traceEnabled , traceWrite , traceCategories , traceError , traceMessageType
88} from "./view-common" ;
99
1010import { ios as iosBackground , Background } from "../../styling/background" ;
@@ -309,7 +309,19 @@ export class View extends ViewCommon {
309309 }
310310
311311 protected _showNativeModalView ( parent : View , context : any , closeCallback : Function , fullscreen ?: boolean , animated ?: boolean , stretched ?: boolean ) {
312- let parentWithController = ios . getParentWithViewController ( parent ) ;
312+ const parentWithController = ios . getParentWithViewController ( parent ) ;
313+ if ( ! parentWithController ) {
314+ traceWrite ( `Could not find parent with viewController for ${ parent } while showing modal view.` ,
315+ traceCategories . ViewHierarchy , traceMessageType . error )
316+ return ;
317+ }
318+
319+ const parentController = parentWithController . viewController ;
320+ if ( ! parentController . view || ! parentController . view . window ) {
321+ traceWrite ( "Parent page is not part of the window hierarchy. Close the current modal page before showing another one!" ,
322+ traceCategories . ViewHierarchy , traceMessageType . error ) ;
323+ return ;
324+ }
313325
314326 super . _showNativeModalView ( parentWithController , context , closeCallback , fullscreen , stretched ) ;
315327 let controller = this . viewController ;
@@ -326,11 +338,6 @@ export class View extends ViewCommon {
326338
327339 this . _setupAsRootView ( { } ) ;
328340
329- const parentController = parentWithController . viewController ;
330- if ( ! parentController . view . window ) {
331- throw new Error ( "Parent page is not part of the window hierarchy. Close the current modal page before showing another one!" ) ;
332- }
333-
334341 if ( fullscreen ) {
335342 controller . modalPresentationStyle = UIModalPresentationStyle . FullScreen ;
336343 } else {
@@ -346,7 +353,8 @@ export class View extends ViewCommon {
346353 parentController . presentViewControllerAnimatedCompletion ( controller , animated , null ) ;
347354 const transitionCoordinator = iosUtils . getter ( parentController , parentController . transitionCoordinator ) ;
348355 if ( transitionCoordinator ) {
349- UIViewControllerTransitionCoordinator . prototype . animateAlongsideTransitionCompletion . call ( transitionCoordinator , null , ( ) => this . _raiseShownModallyEvent ( ) ) ;
356+ UIViewControllerTransitionCoordinator . prototype . animateAlongsideTransitionCompletion
357+ . call ( transitionCoordinator , null , ( ) => this . _raiseShownModallyEvent ( ) ) ;
350358 } else {
351359 // Apparently iOS 9+ stops all transitions and animations upon application suspend and transitionCoordinator becomes null here in this case.
352360 // Since we are not waiting for any transition to complete, i.e. transitionCoordinator is null, we can directly raise our shownModally event.
@@ -356,6 +364,11 @@ export class View extends ViewCommon {
356364 }
357365
358366 protected _hideNativeModalView ( parent : View ) {
367+ if ( ! parent || ! parent . viewController ) {
368+ traceError ( "Trying to hide modal view but no parent with viewController specified." )
369+ return ;
370+ }
371+
359372 const parentController = parent . viewController ;
360373 const animated = ( < any > this . viewController ) . animated ;
361374
@@ -582,14 +595,12 @@ export class CustomLayoutView extends View {
582595}
583596
584597export namespace ios {
585- export function getParentWithViewController ( parent : View ) : View {
586- let view = parent ;
587- let controller = view . viewController ;
588- while ( ! controller ) {
598+ export function getParentWithViewController ( view : View ) : View {
599+ while ( view && ! view . viewController ) {
589600 view = view . parent as View ;
590- controller = view . viewController ;
591601 }
592602
603+ // Note: Might return undefined if no parent with viewController is found
593604 return view ;
594605 }
595606
0 commit comments