Usage of the UIButton control in iOS and interpretation of some parameters, iosuibutton

Source: Internet
Author: User

Usage of the UIButton control in iOS and interpretation of some parameters, iosuibutton

 

In the UI control, UIButton is a type of control that is extremely common. Its Class Object creation is different from that of most UI controls using the instance method init, which is usually created using the class method:

+ (Id) buttonWithType :( UIButtonType) buttonType;

If you use the instance method to create a UIButton object, for example:

UIButton * button = [[UIButton alloc] initWithFrame: CGRectMake (100,300,100, 50)];

There is no problem with object creation, but when you set a title for this button object, such:

[Button setTitle: @ "asd" forState: UIControlStateNormal];

However, the screen is not displayed after the program is run, but this is not a bug in the program, but when the button is created using the instance method, the default color of the title text is white, it is invisible on a white screen or screen without a background color. If you use the class method to create a UIButton object, you need to enter a UIButtonType parameter, which is an enumeration, as follows:

Typedef NS_ENUM (NSInteger, UIButtonType ){

UIButtonTypeCustom = 0, // custom type, that is, no type. When this parameter is selected, the effect of using the class method is the same.

UIButtonTypeSystem, // standard type, usually selected, used in iOS7.0

UIButtonTypeDetailDisclosure, // a blue information flag is added compared to the standard type

UIButtonTypeInfoLight, // The identifier of information is highlighted.

UIButtonTypeInfoDark, // The identifier of information is a bit darker

UIButtonTypeContactAdd, // There is a plus sign more than the standard type

UIButtonTypeRoundedRect = UIButtonTypeSystem, // do not approve of use, has been replaced by the standard type

};

In addition to UIButtonTypeCustom, the default colors of Other enumeration types are blue. When the image is placed on the button, the actual effect is blue.

The custom type correctly displays the original color of the image. In addition, if both the image and text are set for the button, the image is in the left text and in the right text.

Set the title text:-(void) setTitle :( NSString *) title forState :( UIControlState) state;

Set image:-(void) setImage :( UIImage *) image forState :( UIControlState) state;

The function is to display the title and image of the string object in the state. If you want to display different images in different states, the image size must be the same; otherwise, the image will be incorrectly displayed.

The UIControlState is an enumeration type, as shown in the following code:

Typedef NS_OPTIONS (NSUInteger, UIControlState ){

UIControlStateNormal = 0, // normal

UIControlStateHighlighted = 1 <0, // The highlighted state. When the button is clicked, it is changed from normal to highlighted. When the button is released, it is automatically converted to normal.

UIControlStateDisabled = 1 <1, // unavailable status, set through attribute enabled

UIControlStateSelected = 1 <2, // select the transition state and set it through the property selected

UIControlStateApplication = 0x00FF0000, // when the application flag

UIControlStateReserved = 0xFF000000 // reserved for the internal framework

};

Common usage:

[Button setTitle: title1 forState: UIControlStateNormal];

[Button setTitle: title2 forState: UIControlStateHighlighted];

In this way, title1 is displayed in the normal status of the button, title2 is displayed when the button is pressed, and title1.

The same is true for setting images.

Set the background image:-(void) setBackgroundImage :( UIImage *) image forState :( UIControlState) state

Different from the set image, the background image is displayed below the text, rather than on the left.

 

Set the title text color:-(void) setTitleColor :( UIColor *) color forState :( UIControlState) state

This method only changes the title text color without affecting the image color. The tintColor attribute affects both the title text and the image color, but does not affect the background image color. In addition, when UIButton is created, the tintColor attribute is invalid when UIButton is selected as UIButtonTypeCustom or the instance method is used to create the instance.

 

Set the RESPONSE event:-(void) addTarget :( id) target action :( SEL) action forControlEvents :( UIControlEvents) controlEvents;

The target calls the action method when controlEvents occurs. In OC, it sends the action message to the target object.

ControlEvents is an enumeration type, as follows:

Typedef NS_OPTIONS (NSUInteger, UIControlEvents ){

UIControlEventTouchDown = 1 <0, // click the event

UIControlEventTouchDownRepeat = 1 <1, // double-click or multiple-click events

UIControlEventTouchDragInside = 1 <2, // drag an event inside the control window

UIControlEventTouchDragOutside = 1 <3, // event of dragging outside the control window

UIControlEventTouchDragEnter = 1 <4, // drag from outside the control window to enter the internal event

UIControlEventTouchDragExit = 1 <5, // drag the control window to enter the internal event

UIControlEventTouchUpInside = 1 <6, // click the lift event in the control window, after the UIControlEventTouchDown event

UIControlEventTouchUpOutside = 1 <7, // inside the control window, click the outside of the window to raise the event.

UIControlEventTouchCancel = 1 <8, // all click cancellation events, such as too many fingers, locking, or phone calls

 

UIControlEventValueChanged = 1 <12, // event of control value change, mostly used for UISlider, UISegmentControl, UIPageControl

 

UIControlEventEditingDidBegin = 1 <16, // UITextField

UIControlEventEditingChanged = 1 <17, // when the text in UITextField is changed

UIControlEventEditingDidEnd = 1 <18, // when UITextField finishes editing, that is, when the first response is canceled

UIControlEventEditingDidEndOnExit = 1 <19, // when you click 'Return key', the line feed key is used by default, and the edit of the first response is automatically canceled.

 

UIControlEventAllTouchEvents = 0x00000FFF, // all touch events

UIControlEventAllEditingEvents = 0x000F0000, // All UITextField editing events

UIControlEventApplicationReserved = 0x0F000000, // range available for application use

UIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework use

UIControlEventAllEvents = 0 xFFFFFFFF // all events

};

 

Cancel RESPONSE event:-(void) removeTarget :( id) target action :( SEL) action forControlEvents :( UIControlEvents) controlEvents;

The function is to cancel the target's response to action when controlEvents occurs. That is, when an event occurs, the target does not trigger the method action. The premise is that the method response to this event has been added, the parameter can be set to nil, indicating any, all, that is, removing all.

 

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.