我们应该建立自己的代码库,建立自己的工厂

苹果公司给我们提供了强大的利器

可是我们不应该以简简单单的实现基本功能就满足了

大牛的成长之路。都是自己慢慢深入研究

我们要有成长为大牛的目标

今天给大家写个自己定义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

自己定义button的更多相关文章

  1. Android自己定义button实现长按功能

    Android自己定义button实现长按功能 通过自己定义BUTTON,写一个LongTouchBtn类,在按下的时候运行onTouchEvent事件,通过这个事件使用回调函数来实现长按功能! XM ...

  2. Qt之模型/视图(自己定义button)

    简述 衍伸前面的章节,我们对QTableView实现了数据显示.自己定义排序.显示复选框.进度条等功能的实现.本节主要针对自己定义button进行解说.这节过后,也希望大家对自己定义有更深入的了解.在 ...

  3. 宏定义 button 方法 --备

    定义 #define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButto ...

  4. UITableView 自带编辑删除 自己定义button

    一:UITableView 自带编辑删除 1:实现两个方法就可以 #pragma mark   tableView自带的编辑功能 -(void)tableView:(UITableView *)tab ...

  5. iOS总结(自己定义button)

    1.首先新建一个类该类继承UIButton 2.实现几个方法 1).改动字体的一些属性 -(instancetype)initWithFrame:(CGRect)frame{ self = [supe ...

  6. 使用quick自己定义Button

    使用quick时自己封装的类存放于特定的文件夹.便于以后使用 以下是作者经经常使用到的一个按钮 local MyButton = class("MyButton") functio ...

  7. 解决ios7.0 以后自己定义导航栏左边button靠右的问题

    1.自己定义button //左button UIButton *leftBtn = [[UIButton , , , )]; [leftBtn addTarget:self action:@sele ...

  8. 如何让Button使用自定义icon

    1.在Buttton所在的html页面定义button要使用的icon的css样式,如 </style> <style> .dijitArrowIcon { backgroun ...

  9. Tkinter教程之Button篇(1)

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811298 #Tkinter教程之Button篇(1)#Button功能触发事件'''1.一个 ...

随机推荐

  1. 利用ajax,canvas实现的测试php程序占用内存的代码

    receive.php <?php $array["time"]=time();$array["memory"]=memory_get_usage();e ...

  2. JavaScript 面向对象(随笔)

    构造函数创建对象 1构造函数是用new创建对象时调用的函数,与普通唯一的区别是构造函数名应该首字母大写. function Person() { this.age = 50; } let a = ne ...

  3. NSKeyedUnarchiver归档

    把自定义的类对象编码到NSData中 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:bc];//归档,bc是一个自定义的类对象, ...

  4. BZOJ 2659 数学

    思路: 一开始以为是真·欧几里德 a,b来回消 (其实用不了那么麻烦) 我们发现 这是一个矩形 求一下整点数 完了.. 要特判 p=q的情况 //By SiriusRen #include <c ...

  5. B - Beautiful Year

    Problem description It seems like the year of 2013 came only yesterday. Do you know a curious fact? ...

  6. 导入Excel时去除多余的空白行

    https://blog.csdn.net/shuishousuiyue/article/details/44773987 按着上面链接用的第一种方式,如下图所示:第二种方式要遍历每一个Excel单元 ...

  7. 实现model中的文件上传FTP(一)

    由于在django的model中配置了filefield或者imagefield配置了upload_to参数只能将用户上传的文件上传到项目本地,就算重定向到项目外也只是直接读取文件系统,这样对未来的项 ...

  8. String,StringBuffer,StringBuilder效率优先关系说明

    String,StringBuffer,StringBuilder效率优先关系说明: public class StringBufferWithStringBuilder { public stati ...

  9. 「图解HTTP 笔记」Web 基础

    Web 基础 三项构建技术: HTML:页面的文本标记语言 HTTP:文档传输协议 URL:指定文档所在地址 一些概念 HTTP(HyperText Transfer Protocol):通常被译为& ...

  10. 【技术累积】【点】【java】【1】JSONPath

    闲聊 以后周中每天一篇这种偏短的文章,周末就发长一点的文章,不然自己实在是懒,懒成了习惯了... 开始 首先需要明确的是,这里说的是阿里巴巴的fastjson包中的JSONPath,不是jsonPat ...