最近剛開始學(xué)IOS,用的Xcode5中默認(rèn)生成的Storyboard中控件的好多屬性設(shè)置不了,所以就嘗試在空項目里手寫button,如有錯誤,請指正,共同學(xué)習(xí)。
1.新建ViewController類,我起的名字是MCViewController。
2.創(chuàng)建mccontroller控制器,并設(shè)置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 end11 return YES;12 }
3.創(chuàng)建按鈕
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]; //設(shè)置矩形四個圓角半徑12 [btn_enter.layer setBorderWidth:1.0]; //邊框?qū)挾?/span>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 : UIViewController2 -(IBAction)click_btn_enter:(UIButton *)sender;3 -(IBAction)click_btn_quit:(UIButton *)sender;4 @end
5.實現(xiàn)按鈕動作
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 }

新聞熱點
疑難解答