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
How The Economic Machine Works: Part 5
[Music] All of this impacts the central government because lower incomes and less employment means the government collects fewer taxes. At the same time, it needs to increase its spending because unemployment has risen. Many of the unemployed have inadequ…
15 Things To Do Before 11AM To Win the Day
Hey there, Alexir! Now, tell me, how many times have you said, “I wish I had more time in the day”? You’ve got about 16 hours, 960 active minutes, in your day. Are you using that time wisely? Really getting the most out of it? Because if you are, then by …
Path of the Panther | Official Trailer | National Geographic
We called this area Shimmering Waters. [Music] This is our home, just like it’s the home to the deer, the frogs, and the panther. This is our home. [Music] This is the number one cause of death: vehicle collision is number one. In the last two weeks, we …
Lithium Stocks to Soar? Insider Trading Worries? Investing Taxes? - Stock Market Q&A
Hey guys, welcome back to the channel! So in today’s video, we are quite simply doing a Q&A. I sent the message out on my YouTube community tab recently, and you guys left a lot of comments. So unfortunately, I’m definitely not going to be getting thr…
Pick a Business Model With Leverage
One more question about leverage. Do you think a choice of business model or a choice of product can also bring a kind of leverage to it? For example, pursuing a business that has network effects, pursuing a business that has brand effects, or other choic…
An Update on Ray Dalio's Views of The Five Big Forces Shaping 2024
I’m Jim Hasell, editor of the Bridgewater Daily Observations. Earlier this year, we published a Daily Observations by Bridgewater founder and CIO Mentor Ray Dalio, where he described his five big forces framework and how these forces will shape 2024 and t…