IOSApplication新建Empty项目中自定义UIButton

最近刚开始学IOS,用的Xcode5中默认生成的Storyboard中控件的好多属性设置不了,所以就尝试在空项目里手写button,如有错误,请指正,共同学习。

 

 

 1.新建ViewController类,我起的名字是MCViewController。

 

2.创建mccontroller控制器,并设置window的根视图为mccontroller。

 

AppDelegate.m

 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 2 {
 3     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 4     // Override point for customization after application launch.
 5     self.window.backgroundColor = [UIColor whiteColor];
 6     [self.window makeKeyAndVisible];
 7     //自定义code
 8     MCViewController *mccontroller =[[MCViewController alloc] init];
 9     self.window.rootViewController=mccontroller;
10     //code end
11     return YES;
12 }

 

3.创建按钮

 

MCViewController.m

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     // Do any additional setup after loading the view.
 5     
 6     //btn_enter 定义
 7     UIButton *btn_enter=[UIButton buttonWithType:UIButtonTypeRoundedRect];
 8     btn_enter.frame=CGRectMake(20, 360, 280, 40);
 9     btn_enter.backgroundColor = [UIColor clearColor];
10     [btn_enter.layer setMasksToBounds:YES];
11     [btn_enter.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
12     [btn_enter.layer setBorderWidth:1.0]; //边框宽度
13     [btn_enter setTitle:@"点击" forState:UIControlStateNormal];
14     [btn_enter addTarget:self action:@selector(click_btn_enter:) forControlEvents:UIControlEventTouchUpInside];
15     [self.view addSubview:btn_enter];
16     
17     //btn_quit 定义
18     UIButton *btn_quit=[UIButton buttonWithType:UIButtonTypeRoundedRect];
19     btn_quit.frame=CGRectMake(20, 420, 280, 40);
20     btn_quit.backgroundColor=[UIColor clearColor];
21     [btn_quit.layer setMasksToBounds:YES];
22     [btn_quit.layer setCornerRadius:10];
23     [btn_quit.layer setBorderWidth:1.0];
24     [btn_quit setTitle:@"退出" forState:UIControlStateNormal];
25     [btn_quit addTarget:self action:@selector(click_btn_quit:) forControlEvents:(UIControlEventTouchUpInside)];
26     [self.view addSubview:btn_quit];
27 }

 

4.声明按钮动作

 

MCViewController.h

1 @interface MCViewController : UIViewController
2 -(IBAction)click_btn_enter:(UIButton *)sender;
3 -(IBAction)click_btn_quit:(UIButton *)sender;
4 @end

 

5.实现按钮动作

 

MCViewController.m

1 -(IBAction)click_btn_enter:(UIButton *)sender{
2     NSLog(@"I am enter!!!");
3 }
4 
5 -(IBAction)click_btn_quit:(UIButton *)sender{
6     NSLog(@"I am quit!!!");
7 }

 IOSApplication新建Empty项目中自定义UIButton

IOSApplication新建Empty项目中自定义UIButton

上一篇:hdoj 4925 Apple tree 【最小割】


下一篇:pytest用例依赖