UI1_UIButton
//
// AppDelegate.m
// UI1_UIButton
//
// Created by zhangxueming on 15/6/30.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "AppDelegate.h"
#import "MyClass.h" @interface AppDelegate ()
{
MyClass *_myClass;
} @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//UIButton
//按钮
//通过工厂方法创建Button对象
// UIButtonTypeCustom 自定义类型
// UIButtonTypeSystem 系统类型 //ios7之后没有圆角类型的button
// UIButtonTypeDetailDisclosure//详情按钮
// UIButtonTypeInfoLight //信息按钮有一个浅色的背景
// UIButtonTypeInfoDark //信息按钮有一个深色的背景
// UIButtonTypeContactAdd //加号按钮 UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
btn1.frame = CGRectMake(20, 100, self.window.frame.size.width-40, 50);
//设置button的背景颜色
btn1.backgroundColor = [UIColor redColor];
//设置button的标题
[btn1 setTitle:@"按钮一" forState:UIControlStateNormal];
//UIControlStateHighlighted //高亮状态
//设置高亮状态的标题
[btn1 setTitle:@"按钮一被点击" forState:UIControlStateHighlighted]; //设置按钮标题颜色
[btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
//修改标题字体大小
btn1.titleLabel.font = [UIFont systemFontOfSize:30];
//在高亮状态下显示触摸亮点
btn1.showsTouchWhenHighlighted = YES;
btn1.tag = 201;
[btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn1]; UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
btn2.frame = CGRectMake(20, 200, self.window.frame.size.width-40, 50);
btn2.backgroundColor = [UIColor blueColor];
[btn2 setTitle:@"按钮二" forState:UIControlStateNormal];
[btn2 setTitle:@"按钮二被点击" forState:UIControlStateHighlighted]; [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
//设置button tag属性值
btn2.tag = 202;
//按钮添加点击事件
//第一参数:target --- 执行对象
//第二个参数: selector --- 对象中的方法
//第三个参数: event --- 触发事件
[btn2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
// _myClass = [[MyClass alloc] init];
// [btn2 addTarget:_myClass action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn2]; UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeContactAdd];
btn3.frame = CGRectMake(20, 300, self.window.frame.size.width-40, 50);
btn3.backgroundColor = [UIColor cyanColor];
btn3.tag = 203;
[btn3 setTitle:@"按钮三" forState:UIControlStateNormal];
[btn3 setTitle:@"按钮三被点击" forState:UIControlStateHighlighted];
[btn3 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn3]; //自定义按钮
UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom];
customBtn.frame = CGRectMake(20, 400, self.window.frame.size.width-40, 50);
customBtn.backgroundColor = [UIColor yellowColor];
[customBtn setTitle:@"自定义按钮" forState:UIControlStateNormal];
[customBtn setTitle:@"自定义按钮被点击" forState:UIControlStateHighlighted];
[customBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[customBtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[customBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; //设置图片
//设置标题图片
[customBtn setImage:[UIImage imageNamed:@"front.png"] forState:UIControlStateNormal];
//设置背景图片
//设置了背景图片后, 再设置背景颜色不管用
[customBtn setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
customBtn.tag = 204;
//设置是否高亮状态下,背景图片变暗
customBtn.adjustsImageWhenHighlighted = YES; NSLog(@"currentTitle = %@", customBtn.currentTitle); [self.window addSubview:customBtn]; //创建圆角btn
UIButton *roundBtn = [UIButton buttonWithType:UIButtonTypeSystem];
roundBtn.frame = CGRectMake(100, 500, self.window.frame.size.width-200,50);
roundBtn.layer.cornerRadius = 15;
roundBtn.backgroundColor = [UIColor purpleColor];
[self.window addSubview:roundBtn]; self.window.rootViewController = nil;
self.window.backgroundColor = [UIColor whiteColor];
return YES;
} - (void)btnClicked:(UIButton *)btn
{
// NSLog(@"----按钮被点击-----");
if(btn.tag==201)
{
NSLog(@"按钮一被点击");
}
else if(btn.tag ==202)
{
NSLog(@"按钮二被点击");
}
else if (btn.tag==203)
{
NSLog(@"按钮三被点击");
}
else if(btn.tag == 204)
{
NSLog(@"自定义按钮被点击");
NSLog(@"currentTitle = %@", btn.currentTitle);
}
}
//
// MyClass.h
// UI1_UIButton
//
// Created by zhangxueming on 15/6/30.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h> @interface MyClass : NSObject - (void)btnClicked; @end //
// MyClass.m
// UI1_UIButton
//
// Created by zhangxueming on 15/6/30.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "MyClass.h" @implementation MyClass - (void)btnClicked
{
NSLog(@"MyClass 按钮被点击");
} @end
UI1_UIButton的更多相关文章
随机推荐
- 安卓高手之路之PackageManagerservice
源码位置:frameworks/base/core/java/android/content/pm/PackageParser.java 源文件路径:android\frameworks\base\s ...
- 【KPC】关于为什么不用Zepto而用JQuery
1.zepto在window phone手机上不是很兼容 2.JQuery的Deferred对象在zepto上不支持. 3.JQuery经过压缩,以及部分页面的使用,以及缓存,可以达到优化用户体验的效 ...
- [AngularJS] TweenList 3D + AngularJS Animate
AngularJS animations and TweenLite make it really easy to create cool 3d effects in your application ...
- C#的System.ICloneable接口说明
System.ICloneable接口支持克隆,即用与现有实例相同的值创建类的新实例.msdn上的解释很简单,主要就是clone方法的实行,介绍深拷贝和浅拷贝,搞的很糊涂,那么到底是什么意思呢?看看下 ...
- Laravel 5.1使用命令行模式(artisan)运行php脚本
Laravel有内置命令调度器,可以方便的实现Cron. 任务调度定义在app/Console/Kernel.php文件的schedule方法中,该方法已经包含了一个示例.Laravel里有两种方法执 ...
- <转>一道面试题比较synchronized和读写锁
一.科普定义(原文:http://903497571.iteye.com/blog/1874752) 这篇博文的两个主角“synchronized”和“读写锁” 1)synchronized 这个同步 ...
- 【转】详解spring事务属性
转载自:http://blog.chinaunix.net/u1/55983/showart_2091761.html 7个传播行为,4个隔离级别, Spring事务的传播行为和隔离级别[transa ...
- SQL中VARCHAR与NVARCHAR存储区别
DATALENGTH 与LEN的查询区别 插入结果 总结:DATALENGTH计算字节长度,LEN计算字符串长度 VARCHAR(2)是指允许存取字节长度小于或等于2的字符串 NVA ...
- [PHP] htaccess 探秘
.htaccess访问控制(Allow/Deny) 1. 验证是否支持.htaccess 在目录下新建一个.htaccess 文件,随笔输入一串字符(毫无意义),看看什么反应,如果是500错误,说明目 ...
- [Java] 两种发起POST请求方法,并接收返回的响应内容的处理方式
1.利用apache提供的commons-httpclient-3.0.jar包 代码如下: /** * 利用HttpClient发起POST请求,并接收返回的响应内容 * * @param url ...