Use Cocoa basic learning in Xcode and Interface Builder

Source: Internet
Author: User

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:

 
 
  1. #import <Foundation/Foundation.h> 
  2. @interface FileTest : NSObject {  
  3. IBOutlet id numberGet;  
  4. IBOutlet id numberSet;  
  5.  }  
  6.  -(IBAction) CopyNumberFunction:(id)sender;  
  7. @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:

 
 
  1. #import "FileTest.h"  
  2. @implementation FileTest  
  3. -(IBAction)CopyNumberFunction:(id)sender  
  4. {  
  5. float num11,num22;  
  6. num11=[numberGet floatValue];  
  7. num22=num11;  
  8. [numberSet setFloatValue:num22];  
  9. }  
  10. @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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.