//
// 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. 微软ASP.NET网站部署指南(4):配置项目属性

    1.  综述 有些部署设置能够在项目属性里设置的,而且保持到项目文件中(.csproj或.vbproj). 大多数情况下.你都能够在Visual Studio 选择项目属性Project Proper ...

  2. 【摘要】获得HTML元素的绝对位置

    function findAbsolutePosition(obj) { var curleft = curtop = 0; if (obj.offsetParent) { do { curleft ...

  3. 我的CMS心路历程,DNN And Umbraco

    在比较了众多CMS系统后,还是把焦点定在DNN和Umbraco两个系统上,这两个系统都可以使用UserControl扩展自己需要的功能. DNN的架构比较复杂,Module.Skin.Containe ...

  4. [Angular2 Form] Style Validation in Angular 2 Forms

    Inputs using Angular 2’s ngModel automatically apply style classes of .ng-validand .ng-invalid each ...

  5. Unity3d截图保存到Android相册的实现

    Unity3d截图保存到Android相册的实现-----------------------------ultrasoon 季风原创--------------------------------- ...

  6. centos 服务器配置(一) 之端口占用

    1.查找被占用的端口 netstat -tln netstat -tln | grep 8060 netstat -tln 查看端口使用情况,而netstat -tln | grep 8060则是只查 ...

  7. 直接对寄存器操作,实现usart的串口输出寄存器的配置

    就像前面提到的,我用的板子是一款stm32f107系列的板子,在这块板子上,已经开发出了一套比较成熟的库函数,这也就意味着你可以不用直接去操作存储器来实现某些功能.比方说对于USART来说可以直接通过 ...

  8. IFormatProvider,ICustomFormatter,IFormattable总结

    IFormatProvider中 public object GetFormat(Type formatType); 该方法主要用于获取一个 ICustomFormatter接口的实例 ICustom ...

  9. __KERNEL__ macro

    转载:http://blog.csdn.net/kasalyn/article/details/17097639 The __KERNEL__ macro is defined because the ...

  10. Linux高级字符设备驱动

    转载:http://www.linuxidc.com/Linux/2012-05/60469p4.htm 1.什么是Poll方法,功能是什么? 2.Select系统调用(功能)      Select ...