//
VIEWCONTROLLER.M
05- Creating a button with code
//
Created by Vic fan on 2017/7/30.
Copyright Li Hongqiang year . All rights reserved.
//
#import "ViewController.h"
@interface viewcontroller ()
@end
@implementation Viewcontroller
called after the view has been loaded, typically used to initialize ( add) the control, which is the system's active invocation of the
//
-(void) viewdidload {
[Super Viewdidload];
//1. Create a Button object
UIButton *headbtn = [[UIButton alloc]init];
//2. Set the frame of the button
Headbtn. Frame = CGRectMake (+, +, + );
//3. add to view
[self. View addsubview: headbtn];
//4. set up a picture
//4.1 Setting a picture of a normal state
UIImage *nomalimage = [UIImage imagenamed:@ "btn_01"];
[Headbtn setbackgroundimage: Nomalimage forstate:uicontrolstatenormal];
UIImage *hightimage = [UIImage imagenamed:@ "btn_02"];
[Headbtn setbackgroundimage: Hightimage forstate:uicontrolstatehighlighted];
//5. Set Text
//5.1 setting text in normal state
[Headbtn settitle:@ " touch Me " forstate:uicontrolstatenormal];
// set text in highlighted state
[Headbtn settitle:@ " touch me what to do " forstate:uicontrolstatehighlighted];
//6. Set text color
[Headbtn settitlecolor: [uicolor redcolor] forstate:uicontrolstatenormal ];
// Set Highlight status text color
[Headbtn settitlecolor: [uicolor bluecolor] forstate: Uicontrolstatehighlighted];
//7 Click events to add a button
What's the forcontrolevents listening to?
[Headbtn addTarget:self action:@selector(benclicked) forcontrolevents: UIControlEventTouchUpInside];
}
-(void) benclicked{
NSLog(@ " we're almost out of class ");
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
//Dispose of any resources, can be recreated.
}
@end
iOS development-Basic use of code