IPhone Development (6) --- programming customization UIButton

Source: Internet
Author: User

The previous article introduced how to define UIViewController without using the XIB file. In this case, UIButton is automatically created instead of XIB files.

Through this section, we can learn how to use the addTarget method of UIControl and corresponding event actions instead of InterfaceBuilder.

The specific example is based on the CustomViewController class in the previous lecture. When the button is pressed, the counter is incremented and displayed in the view.

First, add the technology variable count in the CustomViewController class.
@ Interface CustomViewController: UIViewController {
Int count; // counter variable.
}
@ End


Next, add the method called when the button is pressed.

-(Void) countup :( id) inSender {
Count ++; // counter auto-Increment
// InSender is the instance of the Button to be clicked, and its title is set below
[InSender setTitle: [NSString
StringWithFormat: @ "count: % d", count]
ForState: UIControlStateNormal];
}


The setTitle method sets the UIButton title. Use forState: to specify the title display status (press, pop up, usually). Here, specify the title of the general status display. Of course, you can also use UIControlStateNormal.

When the Registration button is pressed, the event function can use addTarget: action: forControlEvents: Method in the UIControl class (UIButton inherits the UIControl class, so you can use it directly ). As follows:

-(Void) viewDidLoad {
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor blueColor];
UIButton * button = [UIButton buttonWithType: UIButtonTypeInfoLight];
Button. frame = CGRectMake (100,100,100,100 );
// Processing function when the Registration button is pressed
[Button addTarget: self action: @ selector (countup :)
ForControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: button];
}


ForControlEvents: If UIControlEventTouchUpInside is set in, the response is returned when the button is pressed.

Because the countup type is

1-(void) countup :( id) inSender


When registering, you need to write countup :.

If the function type is

1-(void) countup


The value is countup. The function type received by addTarget is as follows:

1-(void) countup :( id) sender forEvent :( UIEvent *) event


You can also register multiple actions for the same response. For example, the following code registers the above two types of action functions:
// Method 1
-(Void) countup :( id) inSender {
Count ++;
[InSender setTitle: [NSString
StringWithFormat: @ "count: % d", count]
ForState: UIControlStateNormal];
}

// Method 2
-(Void) countup {
Count ++;
}

-(Void) countup :( id) inSender forEvent :( UIEvent *) event {
Count ++;
[InSender setTitle: [NSString
StringWithFormat: @ "count: % d", count]
ForState: UIControlStateNormal];
}

-(Void) viewDidLoad {
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor blueColor];
UIButton * button = [UIButton buttonWithType: UIButtonTypeInfoLight];
Button. frame = CGRectMake (100,100,100,100 );
// Register the first method
[Button addTarget: self action: @ selector (countup :)
ForControlEvents: UIControlEventTouchUpInside];
// Register the second method
[Button addTarget: self action: @ selector (countup)
ForControlEvents: UIControlEventTouchUpInside];
[Button addTarget: self action: @ selector (countup: forEvent :)
ForControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: button];
}


After compilation, It is shown as follows:

 

Author: Yi Feiyang

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.