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 Housing Market Bubble Just Popped
What’s up you guys, it’s Graham here! So we have to talk about what’s going on with the housing market because it was just found out that prices are continuing to go higher. Wait, what? Yep, you heard that correctly! Even though housing starts have dropp…
It’s Rare to Have Competing, Viable, Scientific Theories
Edition that’s similar to Bayesianism, isn’t it? In both cases, they’re assuming that you can enumerate all the possible theories, but you can’t, because that’s the creativity coming in. It’s very rare in science to have more than one viable theory in phy…
Conditions for inference for difference of means | AP Statistics | Khan Academy
A food scientist wants to estimate the difference between the mean weights of eggs classified as jumbo and large. They plan on taking a sample of each type of egg to construct a two-sample t-interval. Which of the following are conditions for this type of…
Making conclusions in a test about a proportion | AP Statistics | Khan Academy
A public opinion survey investigated whether a majority, more than 50 percent, of adults supported a tax increase to help fund the local school system. A random sample of 200 adults showed that 113 of those sampled supported the tax increase. Researchers …
Advanced (plural) possession | The Apostrophe | Punctuation | Khan Academy
Hello Garans, hello David, hello Paige. So today we’re going to talk about plural possession, meaning when more than one person, or thing, or animal owns something else. This, like most other types of possession, tends to involve apostrophes. Makes sens…
A Dry Valley Mystery | Continent 7: Antarctica
Scott Bay’s choppers will be here to pick up the team in 15 hours. It’s an early start this morning, and we’ve got to break the camp down, but not everyone is ready. Yes, I mean science in the Dry Valleys. He’s gone really well; we’ve knocked out pretty …