最近刚开始学IOS,用的Xcode5中默认生成的Storyboard中控件的好多属性设置不了,所以就尝试在空项目里手写button,如有错误,请指正,共同学习。
1.新建ViewController类,我起的名字是MCViewController。
2.创建mccontroller控制器,并设置window的根视图为mccontroller。
AppDelegate.m
1- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions2{3self.window =[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];4//Override point for customization after application launch.5self.window.backgroundColor =[UIColor whiteColor];6[self.window makeKeyAndVisible];7//自定义code8MCViewController *mccontroller =[[MCViewController alloc] init];9self.window.rootViewController=mccontroller;10//code end11returnYES;12}
3.创建按钮
MCViewController.m
1- (void)viewDidLoad2{3[super viewDidLoad];4//Do any additional setup after loading the view.56//btn_enter 定义7UIButton *btn_enter=[UIButton buttonWithType:UIButtonTypeRoundedRect];8btn_enter.frame=CGRectMake(20,360,280,40);9btn_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];1617//btn_quit 定义18UIButton *btn_quit=[UIButton buttonWithType:UIButtonTypeRoundedRect];19btn_quit.frame=CGRectMake(20,420,280,40);20btn_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@interfaceMCViewController : UIViewController2-(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{2NSLog(@"I am enter!!!");3}45-(IBAction)click_btn_quit:(UIButton *)sender{6NSLog(@"I am quit!!!");7}