UIButton is a button, and he inherits UIView. 1: Initialize to size: Convenience builder:
// Initialize UIButton *button = [UIButton buttonwithtype:uibuttontypesystem]; // Set the frame of the Button Button.frame = CGRectMake (+); // Set Background Button.backgroundcolor = [Uicolor redcolor]; // add him to the UIWindow. [Self.window Addsubview:button];
2. Add a Click event
// Set Button caption [Button Settitle:@ " you order me, " forstate:uicontrolstatenormal]; // Click events (Add method) // Implement the button response event in the method selector -(void) buttonaction{NSLog ( @ " you hit me ");}
3. Add a background Image:
[Button setbackgroundimage:[uiimage imagenamed:@ "11.png"] forstate: UIControlStateNormal]; [Button addtarget:self action: @selector (buttonaction:) forcontrolevents:uicontroleventtouchupinside];
Different click picture changes (similar to registration options)
@property (nonatomic,assign) BOOL flag;
Add a Click event method, pass in a parameter, the parameter is the responder of this event, the type of the parameter, is the type of the responder;- (void) Buttonaction: (uibutton *) sender{ if (Self.flag == yes) { [sender setbackgroundimage:[ uiimage imagenamed:@ "22.png"] forstate:uicontrolstatenormal]; self.flag = no; }else{ [sender setbackgroundimage:[uiimage imagenamed:@ "11.png"] forstate:uicontrolstatenormal]; Self.flag = yes;}} 4. Appearance control ①: Foreground picture Note:1 Overlay title 2 does not extrude with buttonframe 3 foreground picture must be a skeleton (image with only a line) (1): Set foreground color picture : [button1 setimage:[uiimage imagenamed:@ "55.png"] forstate:uicontrolstatenormal]; (2): Get foreground picture:uiimage *image1 = [button1 imageforstate:uicontrolstatenormal]; ②: Background color picture (1): Set background color picture: [button1 setbackgroundimage:[uiimage imagenamed:@ "11.png"] forstate: UIControlStateNormal]; (2): Get background color mapSlice:uiimage *image2 = [button1 backgroundimageforstate:uicontrolstatenormal]; ③: Title (1): set title: [button1 settitle:@ "Dot Me" forState:UIControlStateNormal]; (2): Get title: nsstring *title = [button1 titleforstate:uicontrolstatenormal]; (3): Title Color:[button1 settitlecolor:[uicolor redcolor] forstate:uicontrolstatenormal]; ④: Shadow:[button1 settitleshadowcolor:[uicolor orangecolor] forstate:uicontrolstatenormal]; ⑤: Get Color:uicolor *color1 = [button1 titlecolorforstate:uicontrolstatenormal]; 5.delegate Three Step 1, follow the agreement, locate the current class (angle brackets +,uitextfielddelegate in the AppDelegate.h file) 2, set the proxy operations who, who .delegate = Self 3, implement the Proxy method code example:
// Follow the agreement @interface Appdelegate:uiresponder <UIApplicationDelegate,UITextFieldDelegate>
// sets the proxy object. delegate = self;
Implementing proxies (Uitextfield as an example)
-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{ [TextField Resignfirstresponder]; // Release First responder return YES; // To Implement proxy methods Note : // 1 have return value // 2 release first responder }
This is the basic content of UIButton!
UIButton Basic Introduction