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

Objective-C iPhone Programming Lesson 1 | Part 1


6m read
·Nov 3, 2024

Hey guys, this Mac isn't on at the first official iPhone programming tutorial. In the past, I've done Objective-C tutorials of different kinds, but this is the first official one where I'm trying a new method of teaching that I think will actually get to a lot of people better.

I realized when I explained things in too much depth, it confuses people. The way people actually learn this kind of thing is by starting out doing some code and not quite understanding what it does, and then learning what it means later on. That's what I'm gonna be trying to do. I'm also going to be explaining things; don't worry about that, but I'm not going as into as much depth.

Alright, so you have to download Xcode if you don't already have it in order to make iPhone apps the way Apple wants it to be. You can do that by going to developer.apple.com. After you have downloaded it, you want to open it, and I already have done that and created a new project.

So, you're gonna go File > New Project > Application > View Based Application. Real quickly, what a view is, is it's basically an object, that's a technical term, where you can put graphics. For example, the contents of this window is an object because it's got some graphical text, it's got some icons—these are all subviews. This is a subview because it's got like, you can tell it's that little border around that—it's got the scroll thing.

That is, in a brief little description, motive dealers, and view-based applications is where everything's pretty much a view, so I'll be going over that in more depth in a later tutorial. We're gonna call this application "Hello World."

Alright, so if you're familiar with working in an Xcode environment, you know this, but Xcode also comes with something called Interface Builder, which is a very helpful tool. It allows you to use a very easy-to-use user interface to create the views for your app.

If we go to Resources, you can see Hello World View Controller. That's a view controller; it's basically an object that manages a view. I'll also explain that in more depth and why we need those in a later tutorial, but for now, we're just gonna be using one.

So, let's open this XIB for the view control, Main Window.XIB. This is going to be our main view in Interface Builder. To quickly familiarize you with Interface Builder, here's the library window, there's the inspector, and settings, there's the document window, and this is a view editor.

If you're gonna tools, you can see the library and inspector buttons are right there in case you ever can't find those. Under Window, you have the document button. If we take a look at our document, you've got Files Owner. That is the object; that's the view controller.

Alright, you got First Responder, that's where for undo manager and stuff like that; normally you won't use that on iPhone apps. Then, you have your actual view, and this is the main view where the contents of our first screen on the app are going to be.

So, we're gonna drag in some objects from the library. This is a view that's called a UI Text Field; that's the type of object it is. Remember, UI Text Fields are where you enter a line of text. You've got your UIButton, which sends an action—I'll explain that in a second—then you've got your UILabel, which is uneditable text.

I'm just gonna set up our UI so you can stretch this out; you can see Interface Builder is pretty easy to use. It's the first thing I learned, right? We can also set up a placeholder text, that's just a bit of text for yourself, so you can see we've already got a decent looking interface.

I'll explain to you what we're gonna program this interface to do: I'm gonna make it so they type their name there, click "Hi," and this label will say "Hello," then their name. So, it'll say "Hello [Name]."

There are a couple of things we have to do to do this. First of all, you have your code files here. This is the Hello World ViewController.h; that's where you're gonna be talking to Interface Builder through. That's where you're gonna be telling it what you need in your UI, and you can be linking up with that.

There's also where you declare variables and stuff. If you've already programmed in another language, you'll understand this; otherwise, you'll learn it pretty quickly. In Hello World ViewController.m is where you put the actual code, and we will be putting some code in here shortly.

Right now, there's almost no code in here, and that green stuff means it's not code because it's commented out. So, this right here represents this code in the .h; that's simply how it works.

If you write IBOutlet between these two curly braces, this is where you do all your outlets and variables. I'm gonna do a UILabel, you know, like UI TextField, etc. So, let's take a step back. What I just did here was I said IBOutlet; that's the keyword the Interface Builder sees.

The type of variable is a star, which means it's a pointer. We'll get into that. The name is just the keyword that we as programmers can use to refer to our objects. If we go back here, we can see our view; we have two outlets.

But an outlet is something that Files Owner, which is this, can then link to the interface. So, we're going to Files Owner in the inspector. Under the connections tab, we'll have a label connection. We can just click the plus next to that, drag it over to the label.

We have a text field; you actually can just drag that to the text field, so now this code will know about this and this, and they'll be going under the names. Now, how do we set it up so it knows when this button is clicked? That's called an action.

To declare that, we have curly braces. After the closed curly brace, before the @end, so the way IB Actions work is like any Objective-C method. You do IBAction through the parentheses minus parentheses, IBAction, and outside of the parentheses you have hi colon and left parenthesis, and then the sender can really be anything because this is another name for the variable, but we're gonna call that "sender."

This declares that we're going to have a function called "hi." If you know that, a function is something that you should know. So, if we go back to Files Owner, under received actions, you'll see something called "hi." You can drag that directly to the button.

I'll show you another way to do it. If you go to the button, you can go touch up inside, go to the + across the bat, and drag it to Files Owner. The reason I'm doing it this way is so you can think about it in the way that outlets are things that are referenced, and Files Owner is where you link them over to the UI.

Actions are things that the UI gives to the Files Owner, which is why you connect it from the UI to the Files Owner. That's the difference between the two.

Let's write code for this IBAction. Now we can copy the declaration, go into the .m of this. The .m is where you write the actual code. You can paste this, get rid of the semicolon, and here's where we're gonna be fun.

So, let's go over comments real quick since I just did one. If you put //, the rest of that line is not code; it's just a message that you're putting in your code. If you do /*, then everything up to the */ is also a comment, which is why they comment out all these methods.

You could be writing code, but you don't necessarily need to write code in any of them, so you don't need to enter and implement them. There’s also somewhere you have to write some code and stuff like that.

Now that we have done that, we can go here and say, "Well, here's where we write our code."

So, we have two things you can refer to: we can refer to label, and we can refer to text field. We can say label setText: textField.text.

Alright, and if you don't understand what these brackets are, basically this is calling a method, and this is calling a method that has no parameter. Just for now, you don't have to know what this does; it's pretty self-explanatory.

I can't explain it; I have to demonstrate it, and I'll be demonstrating this in another tutorial. For now, all you have to know is left bracket for UILabel or UITextField, space text, right bracket gives you the text in left bracket for UILabel or UITextField.

It's basic text holding; you give it something else as text or string or anything, and then you know the right back. Unfortunately, I had to cut the tutorial short due to YouTube's video length limit, but you can catch the second part of this tutorial on my channel.

I will also put a link to it in the description of this video. Thanks.

More Articles

View All
Cloud-Brained Humanoid Robots Are Right around the Corner | Big Think
The robots of the cartoons and movies from the 1970s are going to be the reality of the 2020s. And there are two real drivers behind this. First is mapping belief space. Historically, it’s been really difficult to be able to instruct robots to do things l…
Alain de Botton: How Proust Can Change Your Life
I think the way to look at literature is as an instrument that sensitizes us to different things. We all know that if five different people are asked to describe one scene, they will all describe it differently. Some will describe the light, others will f…
Describing the invisible properties of gas - Brian Bennett
Transcriber: Tom Carter Reviewer: Bedirhan Cinar Every minute of every day, you breathe without even thinking about it. Your body does it on its own, from the day you’re born until the day you die. You have muscles contract to bring oxygen, a gas, into y…
THE FED JUST CRUSHED THE MARKET | Urgent Changes Explained
What’s up, Grandma’s guys? Here, and welp, it happened. As of a few hours ago, the Federal Reserve yet again raised their Benchmark interest rates by another 50 basis points, officially bringing us to the highest rates that we’ve seen since 2007, right be…
Neil deGrasse Tyson: The 3 Fears That Drive Us to Accomplish Extraordinary Things | Big Think
So about a decade ago I realized that if we were going to go to Mars with people it would be really expensive, and so I thought to myself: what activities have human cultures engaged in, in the past that were as expensive as what it might be to go to Mars…
Perverted Analogy Fallacy: look out for it.
So a person might make a claim like, “Uh, taxation is just because those being taxed have given, uh, implicit consent by continuing to live in a territory which is subject to the tax.” Um, and you’d like to get them to examine whether or not this idea of…