//
// 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的更多相关文章

随机推荐

  1. 将表中数据生成SQL语句

    在开发过程中,经常需要我们对表中的数据进行转移,如果在同台机器,可以使用SQL自带的导入数据,但是如果想让所有的数据生成可执行的SQL语句,它的移植性最强了.首先要设计一个存储过程.具体如下: CRE ...

  2. BBOSS框架使用jquery方式传參到后台的时候,要注意的事项

    BBOSS框架.从前台传到后台的时候,參数要以这样的方式: public String initAddOrModExtendUser(HttpServletRequest request,       ...

  3. png图片那点事

    PNG图片格式现在包含三种类型: 1.PNG8       256色PNG的别名 2.PNG24     全色PNG的别名 3.PNG32     全色PNG的别名 基本上PNG32就是PNG24,但 ...

  4. LINUX 数据结构 &算法 网络协议 & 网络编程 多任务编程

    http://blog.csdn.net/goodluckwhh/article/category/1303091

  5. 关于egg的压缩测试报告

     167274doc  单字分词 全压缩 时间  real 15m58.464suser 13m52.157ssys 2m3.445s   空间 tmpfs 12G 1.5G 11G 13% /ape ...

  6. [原创]SQL SERVER 2008R2 技术总结专题目录索引

    前言:      在工作中使用了SQL SERVER 2008R2已经很长一段时间了,工作中自己也有个蛮好的习惯:总是喜欢将碰到的一些问题.技术方案等记录下来,现在越积越多,最近也比较轻松了,准备整理 ...

  7. Java微框架:不可忽视的新趋势--转载

    原文:http://www.infoq.com/cn/news/2015/06/Java-Spark-Jodd-Ninja?utm_campaign=infoq_content&utm_sou ...

  8. DOS攻击之详解--转载

    源地址没有找到,间接引用地址:http://wushank.blog.51cto.com/3489095/1156004 DoS到底是什么?接触PC机较早的同志会直接想到微软磁盘操作系统的DOS--D ...

  9. js中数组操作

    var selectedCodeArray = []; var num = $.inArray(值, selectedCodeArray)  //值在数组中的位置 selectedCodeArray. ...

  10. C# 之 user32函数库

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...