Errors and crashes with final project in Chapter 17-Presentation Controller & Orientation Animations

I just downloaded the latest files for iOS Animations by Tutorials and have run into the following issues:

  • On first run, it crashes on line 93 of ViewController.swift: let imageView = listView.viewWithTag(i) as! UIImageView (could not cast value of type ‘UIView’ (0x7fff895ce1e8) to ‘UIImageView’ (0x7fff895cde00))

  • After commenting out the cast (// as! UIImageView) the app launches and the transition to HerbDetailsViewController.swift work, but dismissing it crashes on line 39 of PopAnimator.swift: let toView = transitionContext.view(forKey: .to)! (Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value)

Why is ‘transitionContext.view(forKey: .to)’ returning nil?

Hi there,

Please can I check that you’re using the latest edition of the book (6)? In that edition, I’m running this project absolutely fine in Xcode 11.1 GM, and it’s chapter 19 now, not 17.

Thanks

Rich

1 Like

Hi Rich, I downloaded the latest and you’re right, it all works. Were the book and materials updated recently because I was using the ones I downloaded in the past few weeks when I ran into the problem.

Thanks,
Jim

Yes, the iOS13 update is hot off the presses!

What’s changed? I did some debugging, because I had the older one working before, and also have the latest one.

The part below is new. Before it did not have the while loop, and just set the tag once. (The print statement is just me debugging it.)

    if listView.subviews.count < herbs.count {
      while let view = listView.viewWithTag(0) {
        view.tag = 1000 //prevent confusion when looking up images
        print("tag 1000; subviews: \(listView.subviews.count)")
      }
      setupList()
    }

Though there is only 1 subview, it prints twice, meaning it sets the view.tag twice! Evidently it doesn’t “stick” the first time, or it takes a little time to change. I don’t know what this mysterious view is (not a UIImageView obviously), but it was there in the old version too.

Putting that change in the old one fixes it up. Probably best not to ask why that is happening.

The crash is down to differences in the internals of scroll views in the new version of iOS, so there are more subviews. The code wants there to be only one subview with a tag of zero, but that’s the default tag value, so it’s setting all the other subviews to a tag of 1000. The updated code keeps doing this until there are no views with tag 0 tags before building the list, the previous code only did it for the first subview.

This topic was automatically closed after 166 days. New replies are no longer allowed.