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
Brad Templeton: How Bitcoin Disrupts the Finance Industry | Big Think
Should you invest in Bitcoins? I tell people no. I tell people that the value that people are applying to Bitcoins today is definitely speculative. And so that doesn’t mean you can’t make a lot of money; everyone knows what’s called the greater fool theor…
Believe It or Not, Stress Can Be Good For You | Big Think
Most people believe that there is one way that the body responds to stress, you know. Everyone’s heard the fight or flight response. But it actually turns out that that’s just one way that the body and brain can respond to a stressful circumstance. And i…
How the Rich get Richer
So, we’ve all heard the saying: the rich get richer. Looking at the data, it’s easy to see why. The top 1% of U.S. wealth has increased its net worth by 650 percent since 1989, while the bottom 50% only saw its wealth grow a measly 170 percent. The middle…
What the Internet (and Selfies) Reveal About Our Self-Obsession | Big Think.
The contemporary Internet is based on a fundamental lie. We all are told that it’s social. We’re all told that it allows connectivity, allows us to create community. But the reverse is actually true. It’s atomizing us. It’s not creating real community. It…
Evaluating compound boolean expressions | Intro to CS - Python | Khan Academy
How does the computer evaluate expressions with the logical operators and, or, and not to find out? Let’s explore the order of operations for compound Boolean expressions. Imagine we’re working on a program to check if a specific song matches the filters …
Third parties in the United States | US government and civics | Khan Academy
Let’s talk about “third parties” in the United States. I put the word “third” in quotation marks because there’s more than one third party; so you could even think of it as a third, fourth, fifth, sixth, and seventh parties. The reason why people say thi…