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