1 Preface
UIButton is a button control, which is very common in IOS development. You can set events for it.
2. code example
ZYViewController. m:
[Plain]
@ Synthesize myButton;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
MyButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; // set the button style to a rounded rectangle
MyButton. frame = CGRectMake (110.0f, 2000000f, 1000000f, 370000f );
[MyButton setTitle: @ "Press Me" forState: UIControlStateNormal]; // The title when the button is pressed
[MyButton setTitle: @ "I'm Pressed" forState: UIControlStateHighlighted]; // press the button to raise the title
[MyButton addTarget: self action: @ selector (buttonIsPressed :) forControlEvents: UIControlEventTouchDown]; // press the button to trigger the event
[MyButton addTarget: self action: @ selector (buttonIsTapped :) forControlEvents: UIControlEventTouchUpInside]; // trigger an event when the button is pressed and lifted
[Self. view addSubview: myButton];
}
-(Void) buttonIsPressed :( UIButton *) paramSender {
NSLog (@ "Button is pressed ");
}
-(Void) buttonIsTapped :( UIButton *) paramSender {
NSLog (@ "Button is tapped ");
}
@ Synthesize myButton;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
MyButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; // set the button style to a rounded rectangle
MyButton. frame = CGRectMake (110.0f, 2000000f, 1000000f, 370000f );
[MyButton setTitle: @ "Press Me" forState: UIControlStateNormal]; // The title when the button is pressed
[MyButton setTitle: @ "I'm Pressed" forState: UIControlStateHighlighted]; // press the button to raise the title
[MyButton addTarget: self action: @ selector (buttonIsPressed :) forControlEvents: UIControlEventTouchDown]; // press the button to trigger the event
[MyButton addTarget: self action: @ selector (buttonIsTapped :) forControlEvents: UIControlEventTouchUpInside]; // trigger an event when the button is pressed and lifted
[Self. view addSubview: myButton];
}
-(Void) buttonIsPressed :( UIButton *) paramSender {
NSLog (@ "Button is pressed ");
}
-(Void) buttonIsTapped :( UIButton *) paramSender {
NSLog (@ "Button is tapped ");
}
Running result:
When you click the button: