我们应该建立自己的代码库,建立自己的工厂
苹果公司给我们提供了强大的利器
可是我们不应该以简简单单的实现基本功能就满足了
大牛的成长之路。都是自己慢慢深入研究
我们要有成长为大牛的目标
今天给大家写个自己定义button 对于刚開始学习的人来说非常重要
我们要理解苹果公司提供的类库
我们写的程序应该尽量贴近原生
这样我们的程序才会更易理解 效率等方便也会有所提高
上代码了:代码不长,可是是一种思想 要好好理解
// // MyButton.h // 自己定义Button #import <UIKit/UIKit.h> @interface MyButton : UIControl /** * 自己定义的返回自己的类型 * * @param title <#title description#> * @param frame <#frame description#> * @param target <#target description#> * @param selector <#selector description#> * * @return <#return value description#> */ +(MyButton *)createButtonWithTitle:(NSString *)title andFrame:(CGRect)frame andTarget:(id)target andSelector:(SEL)selector; @end
// // MyButton.m // 自己定义Button // #import "MyButton.h" @implementation MyButton +(MyButton *)createButtonWithTitle:(NSString *)title andFrame:(CGRect)frame andTarget:(id)target andSelector:(SEL)selector { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; //UIButton *btn = [[UIButton alloc]init]; btn.frame = frame; btn.layer.cornerRadius =10.0f; btn.layer.masksToBounds = YES; btn.layer.borderWidth = 2.0; btn.backgroundColor = [UIColor colorWithRed:0.18f green:0.64f blue:0.87f alpha:1.00f]; [btn setTitle:title forState:UIControlStateNormal]; [btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside]; return btn; } @end
// // ViewController.h // 自己定义Button #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
// // ViewController.m // 自己定义Button // #import "ViewController.h" #import "MyButton.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; MyButton *btn = [MyButton createButtonWithTitle:@"Happy" andFrame:CGRectMake(10, 30, 100, 50) andTarget:self andSelector:@selector(btnClick)]; self.view.backgroundColor = [UIColor orangeColor]; [self.view addSubview:btn]; // Do any additional setup after loading the view, typically from a nib. } -(void)btnClick { NSLog(@"button被点击了!"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end