yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Objective-C iPhone Programming Lesson 5 - Navigation Apps


5m read
·Nov 3, 2024

Hey guys, this is Mson One with our fifth iPhone programming tutorial.

So before I even get started with this tutorial, some people have told me in the past that my wallpaper is annoying because it changes so much. So in this tutorial, it's not going to be changing; it's going to be the same the whole tutorial. So I'm just mentioning that.

Anyway, I'm going to create a new Xcode project, and we're going to not make it a view-based application, but a navigation-based application. In the previous tutorials, and I believe in tutorial 3, I taught you multiple view controllers. In this tutorial, I'm going to be teaching you how to make a list application where when you click something, something else happens.

We're going to be making a sort of like a sofa catalog, where you'll have a list of different sofas, and you click one to see a picture of it. I have these pictures right here. So now, if we go into my project, you can see it already does a lot of stuff here. I'll just give you a look at some of the stuff it does for us.

And if you run it, it'll already be a fully functioning application with a list already there, and it will even have a title at the top, as you can see in the list. Um, yeah, so we're going to set up a new type of variable in our H file, and this is going to be an NSArray. I'll call it sofas.

What an NSArray is, it's basically a type of variable that contains different other types of variables. So we can now, in viewDidLoad, have our array of sofas contain a bunch of different objects for different sofas. Say sofas equals NSArray alloc and init with objects. We'll say how about Arno, I'm just making this up, retro, how about bed, and how about standard?

We can now, uh, look, and we also have to add nil at the end of this list, because the way you set up an NSArray with objects is you say NSArray alloc and init with objects, you give it a list of your objects separated by commas, and then you have nil at the end, and that's how you set up your array.

Now if we look back in our H file, you can see that this is not a UIViewController; it's a UITableViewController, which is a UIViewController but has responsibilities to it. You have to tell the table view how many rows are in each section, and there's one section, and so there are going to be four, obviously, because that's the number of sofas we have. So we're going to return sofas count, and that'll return the amount of things in sofas.

Now we set it up with these four things: Arno, retro, bed, and standard, so that's going to be how it knows that there are four things. Then right under here is where we have to set up the cell for each index path, which has the row in the section. So we're going to say cell.text = sofas objectAtIndex indexPath.row.

The row of an index path is going to be 0, 1, 2, or 3, um, is the place that we're getting the text for. So anyway, we set up the text of the cell, and now if we run this, you know, we only have to change like two or three lines of code. Now if we run this, it's going to have a list right here, and if you click something, nothing's going to happen.

So the next step we're going to take is we're going to make a new view controller, and this is going to be SofaViewController, and it's going to have an image for each sofa. So now we're going to make it show a new SofaViewController when they click something. Let's go down here, and let's show the view controller almost the same way we show it in a view-based application.

We're going to allocate it, we're going to init it with nibName, and we're going to set up its nibName to this, and the bundle is going to be nil. Then we're going to say self.navigationController pushViewController:SVC animated:YES. Why not? SVC released, now that's just for memory management.

Here we're going to say SVC setTitle, um, and we're going to set it to sofas objectAtIndex indexPath.row. This is just to customize each one, and we're also going to have to do that to the main thing in a minute. Just like, as you can see here, just like in regular view-based applications, we have to import this.

So let's go into our .h, where I like to import things. Let's import this, so now you should watch tutorial 3 if you haven't already to learn about this stuff with the view controllers. I already went over a lot of it.

So now if I click this, it shows a new view. Normally you'd expect the back button, but what that back button normally says is the title of the last screen you were on. If we review, let me just run this again, if we review the app when it opens up, there's no title.

So in viewDidLoad, we're going to set the title of the main screen to be something. So we're going to say self setTitle:@"Sofas." Now if we run this, you'll see here it says "Sofas." We click somewhere, it brings this up, you can go back, go to standard, go back, yeah.

And so now there's one last thing we have to do: we have to work with the images. So for SofaViewController, we're going to say IBOutlets UIImageView *IV, and let's set this up. What we're going to do basically is make it dynamic, sort of, so it will change when they click different things.

So we set it up with Interface Builder. Now let's set up a UIImage. We're going to make an Objective-C method on this called in SofaViewController.h called setNameImage, and we're going to have a UIImage *IMG. This is just like an ID action; it's just slightly different.

We can go into the .m to implement this, and here we're going to say IMG release; IMG equals _IMG retain. And don't worry about the retain release, but what this is doing is we're going to call this before we show the view to set the image. Here in viewDidLoad, we're going to say IV setImage:IMG.

Alright, makes sense. Now here we're going to say we're going to actually set up our images. So let's go here, drag in these images into our resources, and now let's say, if indexPath.row is equal to 0, then we'll say SVC setMainImage:[UIImage imageNamed:@"Arno.jpg"]. I don't know what Arno means, I just sort of found that on the internet.

So anyway, now let's do else if, and we're going to set up all the different images here: set up retro, sofa, bed, and standard. Now hopefully this will work. Um, let's go ahead and test it out.

So we click Arno, gives us a sofa. Click bed, gives us a different sofa, etc., and it's a bunch of different sofas. Now let's make it not stretch by going into this .xib, click there, and say aspect fit instead of scale to fit for our UIImageView just to make that simpler and to make it not stretch the sofas.

So when we click there, it's good. There, it's good. And yeah, we basically have a bunch of different sofas.

So anyway, this is how to make a simple navigation-based application. So, uh, if you like this tutorial, check out our other ones or subscribe. Um, anyway, thanks for watching, Mson One. Subscribe, and goodbye.

More Articles

View All
Michael Burry's $1.6B Bet On A Stock Market Crash?
Michael Barry just revealed what mainstream media is calling a massive bet against the stock market, but in reality, there’s a bit more to it than that. Barry, who has been radio silenced and is deleting his Twitter account, earlier this year has just rel…
Strong base solutions | Acids and bases | AP Chemistry | Khan Academy
When dissolved in water, a strong base like potassium hydroxide will dissociate completely in solution to form hydroxide ions. Potassium hydroxide is an example of a group 1A metal hydroxide. Other examples include lithium hydroxide and sodium hydroxide. …
Going Undercover to Save Manta Rays | Podcast | Overheard at National Geographic
Is a woman in her early 20s, and she is very far from home. Don’t ask where. I’m gonna try to say this without saying, like, country names, because I feel like that could expose me to danger. We can tell you it’s early 2018, and she’s in a major city in E…
The development of an American culture | AP US History | Khan Academy
In this video, I’m going to take some time to talk about the culture of the young United States that developed in the early 19th century. At the beginning of this period, most of the dominant artistic and cultural productions in the United States—the pain…
How to Get Rich Investing in Things You LOVE | Ask Mr. Wonderful Shark Tank's Kevin O'Leary
The question I’m always getting is: what about living? What about spending on things that you love? What about clothes? What about fashion? Do I have to just go Spartan? I can’t buy any of that stuff? Hi, Mr. Wonderful here, and welcome to another episod…
Rewriting fractions as decimals | Math | 4th grade | Khan Academy
Let’s write 29 hundreds as a decimal. So, we’ll start with 29 over 100, and let’s start breaking that down till we can get to place values. Because place values will help us to convert to write this as a decimal. 29 hundreds can be broken down into 20 hu…