Usage of UIButton in iPhone Development

Source: Internet
Author: User

Create UIButton objects in two ways.

(1)

UIButton * button = [[UIButton alloc] initWithFrame: CGRectMake (100, 50,100, 75)];
[Button setTitle: @ "close" forState: UIControlStateNormal];
Button. backgroundColor = [UIColor greenColor]; // the background color of the button.
[Button setBackgroundImage: [UIImage imageNamed: @ "1.png"] forState: UIControlStateNormal]; // The background image of the button

(2)
UIButton * button1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Button1.frame = CGRectMake (200, 20, 50, 60 );
Button1.backgroundColor = [UIColor blackColor];
[Button1 setTitle: @ "clicke" forState: UIControlStateNormal];
[Self. window addSubview: button];
[Self. window addSubview: button1];


Note:The button of the UIButton object created below cannot be displayed in the window.

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

Button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; // redundant
[Button setTitle: @ "close" forState: UIControlStateNormal];
Button. backgroundColor = [UIColor greenColor]; // the background color of the button.
[Button setBackgroundImage: [UIImage imageNamed: @ "1.png"] forState: UIControlStateNormal]; // The background image of the button

Explanation:

1. After creating a button in alloc mode and initializing the button in static mode, the frame information is lost and Memory leakage still exists, the memory generated by alloc For The First Time becomes a part that cannot be recycled. 2. UIButton is special and does not implement the initWithFrame initialization method. This method is a method in the parent class UIView of its parent class and cannot make it a button, if you add a UIView, you can solve the first problem that I mentioned. Therefore, you must initialize UIButton In the first method, and then set the frame to determine its position.
UIButton is used as follows:

// Create a button for the rounded rectangle

UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect];

// The following types of buttons can be defined,

// Typedef enum {

// UIButtonTypeCustom = 0, custom Style

// UIButtonTypeRoundedRect, rounded rectangle

// UIButtonTypeDetailDisclosure, the blue arrow button, mainly used for detailed description

// UIButtonTypeInfoLight, brilliant exclamation point

// UIButtonTypeInfoDark, dark exclamation point

// UIButtonTypeContactAdd, plus cross button

//} UIButtonType;

// Specify the position of the button on the view

Button. frame = CGRectMake (20, 20,280, 40 );

// Hide the button

Button. hidden =! Button. hidden

// Button background color

Button. backgroundColor = [UIColor clearColor];

// Set the button background image

[Button setBackgroundImage: [UIImage imageNamed: @ "PIC"] forState: UIControlStateHighlighted]; // background image

// Set the button to fill the image

[Button setImage: [UIImage imageNamed: @ "btng.png"] forState: UIControlStateNormal];

// Set the button title

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

/* ForState: this parameter defines the status in which the text or image of the button appears */

// The following are several statuses:

// Enum {

// UIControlStateNormal = 0. The normal status is displayed.

// UIControlStateHighlighted = 1 <0, highlighted

// UIControlStateDisabled = 1 <1. The disabled status is displayed.

// UIControlStateSelected = 1 <2, selected

// UIControlStateApplication = 0x00FF0000, when the application flag

// UIControlStateReserved = 0xFF000000 is reserved for the internal framework.

//};

/* By default, when the button is highlighted, the color of the image will be painted a little deeper. If this attribute is set to no, you can remove this function */

Button. adjustsImageWhenHighlighted = NO;

/* Keep up with the above situation. By default, when the button is disabled, the image will be painted a little deeper. Setting NO can cancel the setting */

Button. adjustsImageWhenDisabled = NO;

/* When this attribute is set to yes, the button will emit light when it is pressed */

Button. showsTouchWhenHighlighted = YES;

/* Add events to the button. There are many types of events. I will open a blog post to introduce them. The following time indicates that the event is triggered when the button is pressed and the finger leaves the screen, it is the same as the click event in the web. After this event is triggered, execute butClick: This method. addTarget: self means that this method can also be used to input pointers of other classes */

[Button addTarget: self action: @ selector (butClick :) forControlEvents: UIControlEventTouchUpInside];

// Display Control

[Self. view addSubview: button1];

Note:

[Button addTarget: selfAction: @ selector (alarmTimeDone :)

ForControlEvents: UIControlEventTouchUpInside];

AddTarget: self is linked to self, which is usually set like this
Action: @ selector (alarmTimeDone :) Time Processing Function
ForControlEvents: Message processed by the UIControlEventTouchUpInside control event

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.