InXcodeAndInterface BuilderSimple to useCocoaThis article will introduce the content. Let's look at the content first. The following is a simple program to illustrate how to useXcodeAndInterface BuilderSimpleCocoaDevelopment, learning for beginners.
The program function is to copy numbers in a text file to a lable.
1. Create a project:
Double-clickXcodeProgram icon
If this is the first time you open the welcome page, you can view how to operate it, you can turn it off.
File-> Create Project or: shift + command + N
Mac OS X-> Application-> Cocoa Application-> choose
(Enter the project name at save as) MyTest-> (enter the Project File Save path at Where)-> save
2. Create a class:
(Name of the project you just created) MyTest-> Classes
File-> Create File or: command + N
IPhone OS-> Cocoa Touch Class-> Objective-C Class
(Subclass of) NSObject-> Next
(File Name: Enter the File Name to be added) FileTest. m-> (selected) Also create "FileTest. h"
(Location: the Location of the input file, which may not be in the Project folder)-> (Add to Project select the Project you just created) MyTest
(Select a project in Targets) Mytest-> Finish
3. Create elements in the class:
CocoaThe header file in the language is called interface files, and the code file is called implementation files.
Return to the Xcode interface-> Classes-> FileTest. h (the header file of the newly created class)
Enter a variable in FileTest: numberGet, numberSet
Enter the method CopyNumberFunction before the @ end command.
Save the file command + s)
The result in this example is as follows:
- #import <Foundation/Foundation.h>
- @interface FileTest : NSObject {
- IBOutlet id numberGet;
- IBOutlet id numberSet;
- }
- -(IBAction) CopyNumberFunction:(id)sender;
- @end
4. Create a UI:
Return to the Xcode interface-> Resources-> (double-click) MainMenu. xib (English)
Now that the interface Builder environment is enabled, you can see a window. If you do not double-click Window (Window) in MainMenu. xil (English)
Tool-> Library (which contains all Xcode controls)
(From the Cocoa options) place a Text field, Lable, and Push Button (double-click to change the name) to the window
Tool-> Inspector (used to modify and view the attributes of the selected control and other objects)
5. Bind interface controls and class elements:
Drag the selected Object from Tool> Library> Cocoa to the MainMenu. xil (English) window.
Tool-> Identity Inspector-> select the added Object in MainMenu. xil (English)
Input or select FileTest in class. In this case, the methods and variables in the newly created Class are included in Class Action and class Outlets.
Close the Identity window
Variable binding)
Enter the MainMenu. xib (English) Window-> control + Click (newly added Object) File Test-> drag to the control corresponding to the Window (newly created UI Interface)
{During the drag process, a point is located in the MainMenu. xil (English) window and a line is pulled out. After the Text field control is reached, the Text field is displayed.
Only variables in the FileTest class will appear. Select the control variable to bind}
Method binding)
Method binding is similar to variable binding, but it only needs to be done in turn. Select the Control to be bound according to the Control. Here, the Button-> drag to the MainMenu. xib (English) window.
Find File Test and click the method to bind
6. method implementation:
Return to the Xcode interface-> Classes-> FileTest. m (source file of the newly created class)
Enter the implementation code in FileTest.
The result in this example is as follows:
- #import "FileTest.h"
- @implementation FileTest
- -(IBAction)CopyNumberFunction:(id)sender
- {
- float num11,num22;
- num11=[numberGet floatValue];
- num22=num11;
- [numberSet setFloatValue:num22];
- }
- @end
7. Execute:
Click Build or Build and Go or use the Go project in the menu.
But pay attention to the content in the code to use English half code, such as: the half-width equals sign written into the Utf-8 full angle will compile error.
Summary: InXcodeAndInterface BuilderSimple to useCocoaI hope this article will help you! For more information, see edit recommendations!