The technical term for this is “typecasting”: asking Swift to treat a value as a different type. The solution: we need to tell Swift that what it has is not what it thinks it is. This causes a problem for us because we want to adjust the property we just made in DetailViewController. Swift has the same problem: instantiateViewController() has the return type UIViewController, so as far as Swift is concerned any view controller created with it is actually a UIViewController. But what tickets are they – tickets for a sporting event? Tickets for an opera? Train tickets? The only way for you to find out is to open the envelope and look. I fulfilled my part of the deal: you asked for tickets, I got you tickets. I go off, find tickets, then hand them to you in an envelope. Let’s say you want to go out on a date tonight, so you ask me to arrange a couple of tickets to an event. This will seem confusing if you’re new to programming, so let me try to explain using an analogy. Second, even though instantiateViewController() will send us back a DetailViewController if everything worked correctly, Swift thinks it will return back a UIViewController because it can’t see inside the storyboard to know what’s what. So, we need to use ? just like when we were setting the text label of our cell: “try doing this, but do nothing if there was a problem.” First, we call it on the storyboard property that we get from Apple’s UIViewController type, but it’s optional because Swift doesn’t know we came from a storyboard. The first of those is done by calling instantiateViewController, but it has two small complexities. Set its selectedImage property to be the correct item from the pictures array.Load the detail view controller layout from our storyboard.We can break this task down into three smaller tasks, two of which are new: In the case of ViewController it will be Main.storyboard, which is the same storyboard that contains the detail view controller, so we’ll be loading from there. Every view controller has a property called storyboard that is either the storyboard it was loaded from or nil. When we created the detail view controller, you gave it the storyboard ID “Detail”, which allows us to load it from the storyboard using a method called instantiateViewController(withIdentifier:). That’s the first task done, so onto the second: implement didSelectRowAt so that it loads a DetailViewController from the storyboard. So, add this property to DetailViewController now, just below the existing line: var selectedImage: String? We’ll be setting it straight away, but it still starts off life empty. This property will be a string – the name of the image to load – but it needs to be an optional string because when the view controller is first created it won’t exist. Let’s solve each of those in order, starting with the first one: creating a property in DetailViewController that will hold the name of the image to load.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |