![]() ![]() ![]() Why is that? This operation should never fail. Notice that we use a guard statement and throw a fatal error if the storyboard isn't able to instantiate the MyViewController instance. It is the storyboard ID or storyboard identifier. The string we pass to the instantiateViewController(identifier:) method is the string we defined in the storyboard a moment ago. We load the main storyboard and ask it to instantiate the view controller with identifier MyViewController. Let's instantiate a MyViewController instance and programmatically present it to the user. When the user taps the Code button, the showMyViewController(_:) method is executed. In the Identity section, set Storyboard ID to MyViewController. Select the My View Controller Scene and open the Identity Inspector on the right. The second option to resolve the warning is by defining the storyboard ID or storyboard identifier of the view controller. Choose Action Segue > Show from the menu that pops up.ĭefining a Storyboard ID/Storyboard Identifier ![]() Select the Segue button, press Control, and drag from the button to the My View Controller Scene. The View Controller Scene contains two buttons, Segue and Code. By creating a segue, the view controller is no longer unreachable. A segue defines the visual transition between two view controllers. One option to resolve the warning is by connecting the My View Controller Scene with the View Controller Scene via a segue. We don't want to change the initial view controller of the storyboard, but we have two other options to resolve the warning Xcode outputs. Unchecking the checkbox, removes the arrow. In the View Controller section, the Is Initial View Controller is selected. With the view controller selected, open the Attributes Inspector on the right. The arrow indicates that view controller is the initial view controller of the storyboard. Notice that an arrow is pointing to the view controller in the storyboard. Open Main.storyboard and select the View Controller Scene. Setting the Initial View Controller of the Storyboard Let's take a look at each of these issues in detail. We cannot instantiate it programmatically because the my view controller doesn't have a storyboard ID or storyboard identifier. The My View Controller Scene has no entry points because (1) the my view controller is not the initial view controller of the storyboard and (2) it isn't reachable via a segue. If none of these requirements are met, the view controller is unreachable. is the initial view controller of the storyboard.What you need to understand before we continue is that a storyboard can only instantiate a view controller if the view controller: That should teach you how to use storyboards without becoming frustrated. The goal of this episode of Swift Help is to dissect this warning and understand what it means. The warning reads My View Controller is unreachable because it has no entry points, and no identifier for runtime access view. If we build the project, Xcode warns us that one of the view controllers in Main.storyboard is unreachable. ![]() Main.storyboard contains two scenes, View Controller Scene and My View Controller Scene. The project defines two UIViewController subclasses, ViewController and MyViewController. We are only interested in Main.storyboard. The project has two storyboards, Main.storyboard and LaunchScreen.storyboard. You can download the project if you want to follow along with me. I have created a starter project to get us started. In this episode of Swift Help, I show you how to avoid unreachable view controllers when working with storyboards. Storyboards are great for building user interfaces, but it can sometimes be frustrating to debug the cryptic warnings and errors Xcode throws at you. ![]()
0 Comments
Leave a Reply. |