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

Real Cocoa Programing Lesson 1


5m read
·Nov 3, 2024

Hey guys, this is Ma Head1, and today is going to be a video on some Xcode programming to make an application I like to call EY Copy.

So first of all, you want to open up your Xcode application. You have to have Xcode installed, and it should be the latest version. Then you want to go up to File, New Project, and click under Mac OSX Applications, Cocoa Application. Now I'm going to save it to I Copy.

Okay, so now I can just, um, under EY Copy, I can just open Main Menu.xib to edit the interface. So here's my Interface Builder, and Interface Builder has a library window on the left, an attributes window on the right, a dock window, and a, um, a Main Menu. Actually, these might be reversed and they might just be in any random position, but, um, please ignore that. But to get any of these windows, if they're not up, go under the Tools menu and then click Library or Inspector.

Okay, so now this dock window will go under other windows, as you can see. So you just keep track of it. So now, if you click the Window thing in the dock window, you can edit the properties for the window, and I'm just going to call it I Copy. That's the title of the window. Under Resize, I'm going to uncheck that, and I'm going to make it textured.

Okay, now I'm going to go into my library and find a text field that looks like this, and I'm going to make two copies of it. You can just drag it on twice. Now I'm going to find the, um, button, and I'm going to drag it on, put it in the middle, and I'm going to make this an "IE Clear." Now I'm going to resize the window to be perfect.

Okay, so now we have our interface. So we're going to go up to File, Save. Now we're going to go up to Interface Builder, Quit Interface Builder. So now we want to click on the Classes folder on the left, and then under Action, you say Add New File, and then under Mac OSX, we select Cocoa Objective-C class. We're going to call it AppController.m, and now hit Finish.

Now it's under Classes, and if it's not under Classes, just drag it into Classes. Now we click on AppController. Now we're going to type here inside of this thing that says @interface AppController. Right here is IBOutlet, space, NSTextField, space, star or asterisk, text1. We're just going to copy this line and paste it there again, and we're going to change text1 to text2 on that next line.

So now we have two IBOutlets. Then under this little curly brace, we're going to type - (IBAction) close. Now we're going to say text1Done:(id)sender; semicolon. Now we're going to hit enter, and we're just going to copy that line again, except we're going to change text1Done to text2Done.

Okay, so now, um, what we want to do is we're going to create another third IBAction. Instead of text1Done or text2Done, whatever you've copied, we're going to change that to say Clear. Okay, so it's going to say clearClick. Actually, we're going to do clearClick.

So now we just want to copy these two or three IB actions, and now we can save the file. Save. Now we'll click that m, we'll paste it inside of the implementation, and we're going to get rid of the semicolons and add curly braces to each one like so. I will have this whole Xcode project up for download in the description of this video.

So now in text1Done, we're going to type some code inside of here. We're going to type [text2 setStringValue: [text1 stringValue]]; and I'm sorry, I didn't mean set text, I mean setStringValue. Okay, so that's what I mean.

Okay, so now we're going to do another close bracket there. So this is our final code for this. Okay, so now we're going to go in text2Done, we're going to copy and paste this code we put in there, except we're going to replace text2 with text1 and text1 with text2 so it looks like this.

Now under clearClick, we're going to say [text1 setStringValue:@""]; and inside of the two quotes, we're going to type nothing. Then we're going to do close bracket. We're going to copy that line again and replace text1 with text2. So this way, our clear will clear both text boxes. Text2 will set text1's string value to text2's string value, etc.

So now we want to go back into the main project right here. Now we select Main Menu.xib one more time to launch Interface Builder. So now we want to, in the library, find this blue box called NSObject and we drag this into our dock window. Now we click the object, we go under this little I thing, and under class, we're going to set that class to AppController, which you made earlier.

So now here we have AppController. So now the "IE Clear" button, we're going to click it, hold Control, and down drag it down to AppController. Now we're going to say clearClick. We're going to take AppController, drag it up to the first text box, and we're going to say text1Click. Control second text box, text2. Now, um, now we say click Control from the first text box down to AppController. We say text1Done, and then the same thing with the second text box, except we're going to click text2Done.

Okay, so now we've done all our little linkups. So now I can save this one more time. Save. And now I can quit Interface Builder. So now, if I click Build and Go, hopefully it'll work properly. Let's just wait.

So now, if I type something in here like Alex, and I hit Enter, the other box says Alex. If I type something in here like MacHeads 101, and I hit Enter, this box says MacHeads 101. Now I can also type something in here like hello, and then click this box, and this box will also say hello.

I can click "EY Clear" to clear. Now say I don't want this effect where if you just click somewhere else, it immediately changes the text. All I have to do is go back into my Main Menu.xib files and then select each. First, I'm going to select the first text box, the text field, sorry. Under the um first tab, we're going to go under Action, we're going to say Send on Enter Only.

We're going to do that with the second text field too, and this will have the effect that, um, let me just demonstrate it to you. If you do this and then you click this, it won't change the text of this. Only if you hit Enter on this will it change the text if I say hello.

Okay, but it doesn't matter if you click somewhere else. I personally like the first one more. Maybe in more advanced programming, I'll show you guys a delegate that lets you do it so whenever you press a key in one, it changes the text in the other, but now "I Clear" will still work and stuff like that.

So that's how to make EY Copy in Xcode. Right now, just to prove to you guys that I will put this functional Xcode project online, here's the folder. I'm just going to right-click it and take and press, and I'm going to upload this to our website and have a link to download it in the description of this video.

So, thanks for watching MacInOn. Please subscribe to our videos, it supports us completely, and goodbye.

More Articles

View All
Vincent Kartheiser: Playing William Bradford | Saints & Strangers
[Music] William Bradford was a man who was born in England, and at a very young age, was exposed to church and religion. There were some people on the outskirts of their religion that were beginning to be arrested for their beliefs. In his early adulthoo…
The 2022 Everything Bubble Is About To Pop
What’s up Grandma? It’s guys here, so we did it! If you stayed invested in the markets throughout the last year and not panic sold, you would have been up another 28% without doing a single thing. Or basically, you’d just be break even after inflation. Ju…
DNA cloning and recombinant DNA | Biomolecules | MCAT | Khan Academy
Let’s talk a little bit about DNA cloning, which is all about making identical copies of a piece of DNA. Usually, it’s a piece of DNA that codes for something we care about; it is a gene that will express itself as a protein that we think is useful in som…
Neil and Katy Discuss Fingerprints and Individuality | StarTalk
Why are there seven million people? And why do each one of us have our own fingerprint? Even twins have different fingerprints, who are otherwise genetically identical. Why would you rather we were all the same? No, I’m not. Why is that more odd to you th…
Can We Use Bacteria to Treat Diseases? | Nat Geo Live
( Intro music ) My laboratory gets in and explores, and we really explore a world that’s invisible to the naked eye. And so, if we take a look at these scanning electron microscopy images, you’ll get a closer view. So, we are looking in now, at over 3000…
Coming Home – Ep. 4 | National Geographic Presents: IMPACT With Gal Gadot
GAL: Home is a place where you can find safety and shelter. Kayla knows too well what it’s like to feel unsafe. As a Black trans woman, she has grown up in a world that cast her out for simply being who she is. But she’s determined to leave her truth with…