IPhone DevelopmentProgramming CustomizationUIButtonThe case implementation is the content to be introduced in this article. It mainly describes how to automatically createUIButtonInstead of using 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 article. 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 inherit the UIControl class through addTarget: action: forControlEvents: Method UIButton in 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 Action Function countup) is of
- -(void)countup:(id)inSender
When registering, you need to write countup :.
If the function type is
- -(void)countup
The value is countup. The function type received by addTarget is as follows:
- - (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:
Summary:IPhone DevelopmentProgramming CustomizationUIButtonThe implementation of the case is complete. I hope this article will help you! For more information, see the following articles:
IPhone Development (advanced tutorial) Implementation of iPhone application project composition
IPhone Development 2) iPhone application startup process
IPhone Development (advanced 3) programming and custom UIViewController case implementation