In Xcode, I want to make a screen transition, but it says Could not cast value of type 'UIViewController'

Asked 1 years ago, Updated 1 years ago, 92 views

I would like to do a screen transition on the same storyboard with Xcode

What you want to do
I would like to click on the TableCell of the SessionSelectViewController and pass the value to the SessionViewController when the screen transitions.
I was going to use Segue, but it crashed and I didn't get any error statements, so I'm using a method that doesn't use Segue

Storyboard Image

What's troubling you
In SessionSelectViewController, click

let storyboard:UIStoryboard=self.storyboard!

let vc = storyboard.instantiateInitialViewController() as!SessionViewController // Error here
vc.receiveTitle="hogehoge"
show (vc, sender:nil)

I wrote the code
in the second line. Could not cast value of type 'UIViewController' (0x10d1e21f0) to 'EC2018og.SessionViewController' (0x108676cb0).

I don't know what to do with the error

The transition to the storyboard fails.(Implemented without segue)

As shown in , I tried setting up CustomClass and Module, but it didn't change at all.
I tried to search with the error statement, but I couldn't find a better way.I don't know what the problem is, so could someone please let me know?

SessionSelectViewController

Enter a description of the image here

SessionViewController

Enter a description of the image here

swift ios xcode swift4

2022-09-30 21:33

1 Answers

The transition destination ViewController is instantiated incorrectly.

The storyboard.instantiateInitialViewController() instantiates with the IsInitialViewController designated in the target Storyboard and indicated by a non-left arrow on the Interface Builder as follows:(Only one can exist on one storyboard.)

Initial VC

Your linked article uses a method called instantiateViewControllerWithIdentifier(_:) instead of instantiateInitialViewController().In other words, each View Controller must be named (Storyboard ID) on the Storyboard and instantiated with that name.(Otherwise, iOS won't know which View Controller it wants to instantiate.?)

SessionVC

SessionSelectVC

(Storyboard ID can be anything as long as it is consistent with the program code side.I often use the class name as it is.)

After setting the Storyboard ID correctly as shown above, try rewriting the previous line as follows:

let vc=storyboard.instantiateViewController(withIdentifier: "Session") as!SessionViewController

(The Swift version has been upgraded since the beginning of the article, so the method name looks different.)

However,

I was going to use Segue, but it crashed and I didn't see any error statements, so I'm using a method that doesn't use Segue

Based on the description, it seems that the workaround is not working well because they don't understand how Storyboard and Segue work well and run away as if they don't get any immediate crashes or error messages.

If the above correction successfully solves the problem of your question, please try to create an app that does not crash using Segue.Of course, the stack overflow here should be of some help.


2022-09-30 21:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.