This article uses code to create a button that is divided into 5 steps, respectively:
1, define a button, according to the definition of location can be defined as local variables or global variables;
2, initialization button, generally using a rectangular initialization;
3, set the button control of other properties, such as background pictures, or background color, or button display text and other attributes;
4, add the response event and write the function content of the response event;
5. Load the created control into the window.
Five steps for specific operations see the following figure:
The specific code is as follows:
UIButton *btn;
BTN = [[UIButton alloc]initwithframe:cgrectmake (0, 418, 760)];
UIImage *img = [uiimage imagenamed:@ "fa.jpg"];
[Btn settitle:@ "SDFDSFD" forstate:uicontrolstatenormal];
[Btn setbackgroundimage:img forstate:uicontrolstatenormal];
[Btn addtarget:self action: @selector (haha:) forcontrolevents:uicontroleventtouchupinside];
[Self.view ADDSUBVIEW:BTN];
Response Event code:
-(void) haha: (ID) sender{
NSLog (@ "btn was clicked");
}
The above is the entire content of this article, I hope to help you learn.