Create a button using code in 5 steps, namely:
1, define a button, according to the definition of the location can be defined as local variables or global variables;
2, initialize the button, the general use of a rectangle initialization;
3, set other properties of the button control, such as background picture, or background color, or button display text and other properties;
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 concrete operation see:
The specific code is as follows:
1UIButton *btn;2BTN = [[UIButton alloc]initwithframe:cgrectmake (0, -,418,760)];3UIImage *img = [UIImage imagenamed:@"fa.jpg"];4[BTN Settitle:@"SDFDSFD"Forstate:uicontrolstatenormal];5 [btn setbackgroundimage:img forstate:uicontrolstatenormal];6 [btn addtarget:self action: @selector (haha:) forcontrolevents:uicontroleventtouchupinside];7[Self.view ADDSUBVIEW:BTN];
Response Event code:
1 -(void) haha: (ID) sender{2 NSLog (@ "btn was clicked " ); 3 }
iOS Development-ui Learning-Create a button with code