This course is going to explore table views, which are views that allow you to display data in your arrays. Then we're going to put this knowledge into practice by building an app called Landmark Book.
Intended Audience
This course is intended for beginners who want to learn how to build apps for iOS.
Prerequisites
To the most out of this course, you should have some basic understanding of programming and computer science in general.
Hi. Within this lecture, we're going to finish our app by passing this information to our image view controller. So, we can show the image and the name of the selected landmark to our user. And in fact, we know how to do that, right? We have seen how to pass information using segues within the previous lectures. So, in order to do that, I'm going to create two variables in here and I'm going to assign those variables to my landmarkLabel text and image view image. So, let's come over here under our class but outside of our viewDidLoad. So, I'm going to call first one is landmarkName or chosenLandmarkName or selectedLandmarkName. Okay so this will be a string but empty string initially. And I'm going to create a selectedLandmarkImage as well. This will be a UIImage but this will be an empty image. I'm not going to assign any value to that. So, under viewDidLoad, I can just go for and say landmarkLabel.text is actually selectedLandmarkName. And selected or landmarkImageView, actually imageVew.image is the selectedLandmarkImage. So, this will get the landmarkName and the image from the other viewController and display those values in our Views. So, how do we pass that information? Maybe, you remember the function prepareForSegue?
That is one of the last things that will happen when we commit for a segue so that we can assign our second viewController. Our imageViewController is a variable and then, we can reach the variables inside of that second ViewController and we can change those variables in a way that we want. So, before we do this performSegue thing, we have to find the selected row. We have to get this from the index path. And we have to assign those values to some variables so that we can use those same variables we didn't prepare for segue function. So, I'm going to create a chosenLandmarkName. I'm going to make this equal to landmarkNames[indexPath.row]. So, if a user taps on this 'Colosseum', it will be landmarkName zero. And my chosen landmarkName variable will be Colosseum. I'm going to do the same thing for image as well. So, I'm going to go for chosenLandmarkImage and this will be a landmarkImages[indexPath.row]. So, right now, this landmarkName and landmarkImage is giving me the chosenLandmarkName and landmarkImage with the help of indexes. So, for example if I choose Colosseum, it will be index zero and I will get colosseum jpg and colosseum name.
So, next thing is to pass this information using prepareForSegues I explained before. If any reason, you missed the previous sections, I suggest you go back and watch them because we have seen this prepareForSegue and other some advanced iOS features within the previous sections. So, let's call this prepareForSegue in here. Okay, in order to do that, you can just write prepare and you will see this for segue function. And this is not even related to tableView. This is not related to any delegate. We can just use this in any view controller we want because we're doing a Segue here, where we're just passing information from one view controller to another and we can use this Segue variable that is given to us within this function and we can control this identifier. We can just say if segue.identifier is actually "toImageViewController". Then, we're going to do some things. So, let's open our curly braces. And what we want to do is to make sure that this identifier is actually the same as we have defined before. If they do not match, this won't work. So, make sure you're writing this in the right way. Okay, so if segue.identifier is exactly the way we defined before, then what we want to do, we want to define our ImageViewController as a variable in here. And remember, how we can do that. We can just say let destinationVC which stands for view controller, segue.destination. Okay segue. So, this gives me a UIStoryboardSegue. And the destination will give me a UIViewController and I'm certain that this will be my imageViewController. So, I'm going to first cast this as imageViewController. So, I can use this because I know my segue identifier is toImageViewController and this will land me in imageViewController file. So, now I can use this destinationVC to reach imageViewController's variables. Once you do that, I suggest you do a command B to build and synchronize everything inside of your project. Or maybe, you cannot see these images, these variables inside of this destinationVC. If you can't see them, there is no problem. Go ahead. So, if you say destinationVC.selectedLandmarkName or destinationVC.selectedLandmarkImage then, we can change them now. So, what do we want to assign here? We want to assign chosenLandmarkName and chosenLandmarkImage in here. But I cannot do that right now because I have defined and initialized these variables inside another function. So, best course of action here is to delete this and do this inside of my class, but outside of all the functions. So, I'm going to come up here and I'm going to say selectedLandmarkName. So, this will be an empty string initially and selectedLandmarkImage, then this will be a UIImage and this is an empty image initially as well. So, let's go for here. And this was chosenLandmarkNames. So, let me just delete this and write chosen and in fact, it doesn't matter. But I just want to avoid any confusion because we have named other ones as selected as well. So, this will be chosenLandmarkName and this will be chosenLandmarkImage. Okay, and now I can come down here and I can say destinationVC.selectedLandmarkName is actually chosenLandmarkName. And destinationVC.selectedLandmarkImage is actually chosenLandmarkImage.
So, it means that we're getting the chosenLandmarkImage, Name and ImageView itself and we are assigning them in the second ViewController so that we can show them to the user. So, let's see, Colosseum. Here we go, Colosseum. Let's try each one of those landmarks so we can see Kremlin, Stonehenge and let's go for Taj Mahal, so that's perfect. That's what we wanted in the first place. So, again this is a basic application but this gives us a great template to work upon. So, right now, you can add whatever you want here. You can add a long description, you can add a map. Later on, you're going to see how to do that. You can add the country, you can add whatever you want. You can build a good app with this template. And last but not least, I'm just going to show you how to add a navigation bar title so that you can have a title in your app. In order to do that, let's go to viewDidLoad of our ViewController and just say navigationItem. Okay, dot you can just say title here and you can choose whatever title you want so that you will have this title in your own app. So, let's run this and you will see what I mean. As you can see, we see the Landmark Book in here. So, this is a little bit better looking and it gets to your back button as well. So, back button is called Landmark Book now. So, I hope you enjoyed this section. We're going to stop here and within the next section, we're going to go ahead and learn intermediate, swift concepts.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.