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

Objective-C iPhone Programming Lesson 3 - Multiple Views


5m read
·Nov 3, 2024

Hey guys, this is Back Kids along with our third iPhone programming tutorial. In the previous tutorials, we've been working with one screen that the user sees. It doesn't change; it will have a texting or something like that on it and it did some stuff.

In this tutorial, I'm going to be showing you how to make multiple screens or views that will allow the user to navigate through menus, stuff like that. So, we're gonna go into Xcode, create a new project, create a view-based application, and I'll call it "Multiple Views Controllers."

In previous tutorials, we've been writing our code in the main view controller, and we've been editing the main view controller's interface. But the thing with, you know, bigger apps that have menus and multiple screens and different levels, and games possibly, is that they have more than one view controller. A view controller essentially contains a view that's the full size of the screen that we can click interface on, and they also support stuff like animations, you know, all this nice stuff that makes it look nice.

So let's set up the screen, or the view controller, that the user will see when the app first opens, and that's in the Main View Controller. Alright, so I'm gonna have a button in the view that says "Next View," and I'll even have a title thing that says "Alright."

What we're gonna do is make it so that when they click this button, another screen pops up, and it's got a different sort of interface. You can go to "Multiple View Controller." We'll just set up our action for the button here and cheers. This is where the code will go to bring up another view controller that will have a different interface.

So, we already know how to use Interface Builder to create the main view of the main interface. But what we don't know how to do is create a new view controller. What you want to do is right-click on Classes, go to "Add New File," go to Cocoa Touch Classes, you want to do Controller Subclass. I'm going to set it up like this, and I'm gonna call it "Next Controller."

We'll just drag the XIB and resources to make it easier to understand. And here's the screen, here's the view controller that the user won't see when the app first comes up. This is going to be what appears when they click the "Next View" button.

On this view, they will add a button to this that is "Done." By the way, the buttons that look like this work the same way that regular UI buttons work. So, you can go here and our Next View Controller's actions. Well, you have to understand this; it will get sort of confusing if you're dealing with tons of views and you have them all open in Interface Builder at once, and you sort of lose track of which not eh and not em you're using.

So, it's very important to keep track of that. I'll go back into Interface Builder and hook up this button with our Next View Controller, and I'll hook up the "Next View" button in the main view to the next action.

So, we've now set up the actions in both this and this. What we’re gonna do now is make this code show the other Next View Controller. You know, it's not as straightforward as you think. It's not one line of code, but it's pretty easy. All you want to do is declare a new instance of Next View Controller (MVC), and we will set it up.

I'll explain this to you in a second because it's actually nice to understand this stuff. Alright, so before we compile, there's one other stuff I have to do. Before that, we can take a look at what we're doing. This is declaring a new variable basically that is the type Next View Controller. We made a new class, a new view controller that is an object called "Next View Controller," and that's the type of object it is.

An MVC is just an instance of that, just like if we say int I, that's an instance of an int. Now, that's about examples because it isn't actually an object. What it's pretty similar to is Next View Controller, which is a type of the UI View Controller. It's what we call a subclass, meaning that it does everything that a UI View Controller would do, plus more.

If we take a look at this, this would work if we just said UI View Controller. Let's write that as well. It's the same thing; it's just more stuff. And every UI View Controller responds to a lock, and it responds to init with nib name bundle. You can set the nib name to nil, and it'll still work, and bundle to nil, and it will still work.

And that’s magic stuff Apple dust rated from the nuts; it’s really nice. Alright, the next line of code is self present modal view controller MVC animated. Guess you might be wondering, well, what is self, and what's present, etc.? Self, once again, represents an instance of UI View Controller.

Self basically means whatever class we're writing this in. First, itself. So, multiple View Controller's View Controller is a type of object, as well as Next View Controller. You know, basically, it works just like UI View, and so if we were to declare MVC, which is a type of UIViewController right there, it would be the same.

So, let's just fix that. So right there, does that modal view controller animated is a function on every UI View Controller, as well, that basically says, "Alright, I want you to display this new UI View Controller on the screen, and I want you to animate it." In this case, and then every object responds to the release method, which just frees it in memory if it's not rotated.

There's one more step also you have to take with this in order for it to work. Right here, we're referring to the Next View Controller, but in order to know about XTU Controller, we have to import it. So, I'll go into Multiple View Controller’s View Controller dot H, and in here next to this thing's board, but another important can port that.

And you'll find when you have a lot of objects and classes set up, you'll be importing them all over the place, and it'll be somewhere confusing. So now, if we run this, I'll just demonstrate it. Click "Next View," and it brings up this new view. Now, the "Done" button doesn't work yet, and we haven't set that up right.

So now let's set up the "Done" button. We can go into Next View Controller, and we've already got this "Done" button's action set up. All we're gonna do is do stuff and P Controller Smiths, etc.

What this basically does, self once again in Next View Controller represents an instance of UI View Controller, and every UI View Controller has a parent View Controller property. We'll get into properties and work and some more object-oriented stuff, and then the Smiths modal view controller animated is a function that hides the View Controller, and you can pass YES to that.

So now, if we run this, click here, it comes up, we can click "Done," and it hides. So that was how to do multiple Views, View Controllers with iPhone Objective C. If you have any questions, I know I did this pretty fast. Just leave comments below, and I'll try to answer them as fast as possible.

So thanks for watching, Mekansm alone, subscribe, and give back.

More Articles

View All
The truth about my $78 per month Tesla
What’s up you guys, it’s Graham here. So I rarely ever make follow-up videos like this, but given the amount of views last Tesla video got and also a lot of the misunderstandings with that video when it comes to tax write-offs, how they’re applied, and ho…
Robinhood Just Got Cancelled - Again
What’s up you guys, it’s Graham here. So historically, they say that on average September is the worst month for the stock market, dating all the way back to 1950. Now whether or not that comes true for this month is yet to be seen, but I have to say the…
Can you solve the giant spider riddle? - Dan Finkel
Once every century, the world’s greatest spiders gather to compete in a series of grueling games. The winner will become the next arachnomonarch, able to command all the world’s spiders to their will. That day is today, and for the first time, you’re cast…
Your money trauma starts at childhood | Your Brain on Money
From a child’s perspective, you know, everybody’s bigger, the world is scary, they’re not sure how to navigate. The brain is taking in information; is making narratives from an early age. They are looking to the people around them to show them how to in…
I grew from ZERO TO 100K SUBSCRIBERS in 3 MONTHS (& how you can too)
Imagine a life where you have complete control over your time, location, and finances. Well, the world is your oyster, and every day is an opportunity to live your dreams. But if I told you that you can achieve all this by doing what you love and sharing …
Nicholas Negroponte: Satellites Could Bring Everyone Online | Big Think
Education is a human right for sure. And Internet access is such a fundamental part of learning that, by extension, it is almost certainly a human right. Within a very short period of time, it will be particularly important because of those who don’t have…