IPhone Development Study Notes 001-how to associate controls with codes on the Xib Interface

Source: Internet
Author: User

(Note: Environment Mac OS X Lion 10.7.3 + Xcode 4.2.1 + iOS SDK 5.0 .)
For example, if a sub class of UIViewController is added to the project and "With XIB for user interface" is selected when the new class is created, three files: xxx. h, xxx. m, xxx. xib (also in the early stage *. nib, xib, and nib both refer to the UI source files. Later, apple used the xib extension ).
Default content of xxx. h:
# Import <UIKit/UIKit. h>
@ Interface xxx: UIViewController
@ End

Default content of xxx. m:
Nothing except the interface automatically added by the project.
*. By default, xib has a view, which is the default view of UIViewController and is not added to any other subview. drag two controls from the Xcode Object Library to the view: a label and a button.
Last example:

Then, a UILabel and UIButton are added in xxx. h as attributes. In this case, Xcode automatically generates two circles next to the attribute definition, for example:



 

We can see that the member variables mentioned above have been commented out. In fact, it must be written in earlier versions, the corresponding @ property and @ synthesize property accessors use the getter setter method to operate on these two member variables. Later versions of member variables do not need to be written like this. That is:
(1) Previous versions:


Header file:
@ Interface xxx: UIViewController {
UILabel * label;
UIButton * button;
}

@ Property (nonatomic, retain) IBOutletUILabel * label;
@ Property (nonatomic, retain) IBOutletUIButton * button;
@ End

*. M source file:
@ Implementation xxx
@ Synthesize label;
@ Synthesize button;
...
(2) Xcode 4.2:

Header file:
@ Interface xxx: UIViewController {
// UILabel * label;
// UIButton * button;
}

@ Property (nonatomic, retain) IBOutlet UILabel * label;
@ Property (nonatomic, retain) IBOutlet UIButton * button;
@ End

*. M source file:
@ Implementation xxx
@ Synthesize label = _ label;
@ Synthesize button = _ button;
...
Later versions indicate that the default attribute access methods operate on the _ lable and _ button member variables. The header file is saved, of course *. m can still be written as before, without adding = _ label and = _ button in the back. It should be handled by the compiler by default. There are not many details.

Now there are labels and buttons in the source code, and the corresponding xib files also have labels and buttons. Now we want to associate them accordingly. After all, some dynamic effects often require code to achieve. Now we will introduce the following methods:


Method: "drag"
For example:

 

Pay attention to the Editor position on the top right, and hit the view to the assistant editor so that the display mode is changed to xxx on the left. xib, xxx on the right. h. Click label property on the right to declare the circle on the left. Press and hold the circle and move it to the Label control in the xib view area on the left. Xcode generates a line. Releasing the link completes the link between the label and the label attribute in the Code on the interface. The same applies to the button. After the connection, the corresponding small circle becomes solid. When you move the cursor over the interface, it will become a plus sign (+), and the control that has been connected to the interface will be displayed on the xib interface on the left.
For example:



 

This is the so-called drag method, and then you can go *. the m file controls the display style of the control at will, including the background color, background image, rounded corner, font size, and displayed text.

Supplement:
I recently accidentally saw a blog post by Daniel on the Internet. I have a new understanding of the xib binding principle in Xcode4. The following is an excerpt from the original article:
1. Step 1: Set the class attribute of the File's Owner of the xib File:
In the xib File, the most important thing is File's Owner. This object refers to the Class object set in the Class attribute. Only classes in the Class attribute can be bound to controls and IBOutlet and IBAction on the xib interface. In other words, you must first check whether the attribute Class of the File's Owner in the xib File is set correctly, and then set the corresponding Outlet and Action. That is, after setting the object class corresponding to the File's Owner, Xcode will know which IBOutet and IBAction the object class has, in this way, the IBOulet and IBAction defined in the header file of the object class will be displayed when the link is dragged.


2. Step 2: declare IBOutlet and IBAction in the Class header file specified above:
To declare IBOutlet and IBAction in the header file, let the compiler know that these attributes and event calls are called to the interface. IBOutet is to read the objects in the interface (xib) to the Implementation class, so that you can set the properties of these interface controls in the implementation class, so as to change to the display of the interface.




3. Step 3: bind the File in the xib Editor (IB) (select the File's Owner of the xib, and switch the Utilities window to the connections inspector tab)
Set Outlets under the connections inspector tab (if IBOutlet is declared in the header file of the specified class), and Received Actions (if IBAction is declared in the header file of the specified class) drag and Drop the control in the Objects in the middle window.
You can edit and bind the interface by left-clicking the associated interface control and the IBOulet or IBAction of the implementation class.
Put two images:
Outlets:




Received Actions:




 

Thanks again for this blog!

 

From Code Heaven

 


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.