UIButton button class
Use of UIButton button class
What do I want to say?
1. What is a button?
2. Basic use of buttons
Knowledge points
1. What is a button?
As shown in the following figure, we often need to allow users to control our applications.
2. Button usage
// Use of the button class UIButton // 1. create a button // create a button UIButton * button = [UIButton buttonWithType: UIButtonTypeSystem]; // set the location button. frame = CGRectMake (100,100,100, 30); // set the text [button setTitle: @ "Click me" forState: UIControlStateNormal]; [self. window addSubview: button]; // description 1: the button is generally created using the buttonWithType class method, the parameter is of the button type // The following two types are most commonly used // <2> UIButtonTypeCustom image button // <1> UIButtonTypeSystem system button // description 2: when setting the displayed text, parameter 2 is in the status. // The UIControlStateNormal is usually used to indicate the normal status. // Note 3: // ios6: The default value is the rounded Rectangle Button. // ios7: all buttons have no borders (flat). // indicates the method to be executed when the button is clicked. // forControlEvents control event type // common events: press // addTarget and action to process the event [button addTarget: self action: @ selector (btnClick) forControlEvents: UIControlEventTouchUpInside]; // 2. set text color [button setTitleColor: [UIColor greenColor] forState: UIControlStateNormal]; // 3. set the font button. titleLabel. font = [UIFont systemFontOfSize: 24]; // 4. disabled button (NO indicates disabled) button. enabled = YES; // 5. highlighted button when clicked. showsTouchWhenHighlighted = YES; // 6. set the Tag value // each control has the tag property // assign any value to this property to distinguish the event handling methods of different controls // 10 buttons are all a button. tag = 100; // 7. implement the buttons UIButton * imageButton = [UIButton buttonWithType: UIButtonTypeCustom]; imageButton. frame = CGRectMake (100,200,100, 30); // Note: When you upload the image resource you use to the project, UIImage * image = [UIImage imageNamed: @ "back.png"]; // click the button to add the background image [imageButton setBackgroundImage: image forState: UIControlStateNormal]; // set the image on the left of the text [imageButton setImage: [UIImage imageNamed: @ "logo.png: UIControlStateNormal]; // 8. set the offset (location) of the image and text in the button. // UIEdgeInsets // top, left, bottom, right // you can control the image and text on the top of the button. On the left, the distance between the bottom side and the right side of imageButton. imageEdgeInsets = UIEdgeInsetsMake (0, 60, 0, 0); imageButton. titleEdgeInsets = UIEdgeInsetsMake (0,-60, 0, 0); [self. window addSubview: imageButton]; [imageButton addTarget: self action: @ selector (btnClick) forControlEvents: role]; [imageButton setTitle: @ "" forState: UIControlStateNormal]; self. window. backgroundColor = [UIColor grayColor]; [self. window makeKeyAndVisible]; return YES;} // Add a processing method for clicking an event for the button. // The parameter is fixed and the parameter is the event source-(void) btnClick //-(void) btnClick :( UIButton *) button {// NSLog (@ "I Have Been clicked ..... "); // a dialog box is displayed: UIAlertView * alertView = [[UIAlertView alloc] init]; alertView. message = @ "the button has been clicked"; [alertView addButtonWithTitle: @ "cancel"]; [alertView show];}