Real Cocoa Programing Lesson 1
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.