About inheritance relationships for controls (interview highlights):
( 1 all controls inherit from the UIView .
( 2 ) can listen to events are inherited from the first Uicontrol , Uicontrol again inherit from UIView . Like UIButton.
( 3 ) can be sliding in the whole is inherited from Uiscrollview , Uiscrollview again inherit from UIView . Like UITableView.
( 4 ) UIWindow is also inherited from UIView .
First, the code to create the control is written in
-(void) viewdidload{
};
, this method is the method that executes after the view has been created.
1. Create a button
UIButton *btn = [[UIButton alloc] init];
2. Add a button to the view
[Self.view ADDSUBVIEW:BTN];
3. Set frame (sets the position and size of the control)
Btn.frame = CGRectMake (50, 50, 100, 100);
4. Set the background image
(1) Loading images by file name (all PNG images, do not add extended name)
UIImage *normal = [UIImage imagenamed:@ "btn_01"];
(2) Set background image in normal state
[Btn Setbackgroundimage:normal Forstate:uicontrolstatenormal];
(3) Load the highlighted picture
UIImage *high = [UIImage imagenamed:@ "btn_02"];
[Btn Setbackgroundimage:high forstate:uicontrolstatehighlighted];
5. Set text
[Btn settitle:@ "point me," forstate:uicontrolstatenormal];
[Btn settitle:@ "Touch me what to do" forstate:uicontrolstatehighlighted];
6. Set Text color
[btn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];
[btn Settitlecolor:[uicolor Bluecolor] forstate:
Uicontrolstatehighlighted];
7. Tap the Monitor button
[btn addTarget: Self action: @selector (btnclick:) forcontrolevents:
UIControlEventTouchUpInside];
"Remarks" * A control can be found by tag, for example:
UIButton *button = (UIButton *) [Self.view viewwithtag:10];
Because the return value of [Self.view Viewwithtag:10] is uiview, it needs to be cast to UIButton (UIButton *).
* For listening button click
[Btn addtarget:self Action: @selector (Btnclick:) forControlEvents:
UIControlEventTouchUpInside];
The Addtarget parameter represents which view,action represents which method in this view.
iOS development to customize a control by code