UIButton 按钮的介绍,属性以及使用
//
// ZKAppDelegate.m
// 04-UIButton
//
#import "ZKAppDelegate.h"
//匿名分类
@interface ZKAppDelegate()<UIAlertViewDelegate>
@end
//设置tag值
typedef enum{
ButtonWithOne = 1,
ButtonWithTwo
}ButtonTag;
@implementation ZKAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//创建按钮(UIButton)
//1。创建按钮
/**
按钮样式:UIButtonTypeSystem 是iOS7.0之后的代表系统的按钮样式
UIButtonTypeRoundedRect (圆角矩形)是iOS7.0以前在一直使用的系统样式
*/
//UIButton *button1 = [[UIButton alloc]init];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
//2.设置显示的文字
//注意:在给按钮设置属性时,最好使用set方法,不要使用。语法
//UIControlStateNormal 正常状态
//UIControlStateHighlighted 高亮状态
//UIControlStateSelected 选中状态
[button1 setTitle:@"点我" forState:UIControlStateNormal];
[button1 setTitle:@"点中我了" forState:UIControlStateHighlighted];
//状态为未选中
button1.selected = NO;
[button1 setTitle:@"选中我了" forState:UIControlStateSelected];
[button1 setShowsTouchWhenHighlighted:YES];
//3.设置位置
button1.frame = CGRectMake(10, 40, 100, 40);
//4.显示
[self.window addSubview:button1];
//5.执行方法
[button1 addTarget:self action:@selector(clickedButton:) forControlEvents:UIControlEventTouchUpInside];
//按钮的属性设置
//UIButtonTypeCustom:自定义类型的按钮
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
//1.位置
button2.frame = CGRectMake(10, 100, 100, 40);
//2.设置显示文字
[button2 setTitle:@"快点我" forState:UIControlStateNormal];
//2.2 文字颜色
[button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//2.3文字大小
[button2.titleLabel setFont:[UIFont systemFontOfSize:20]];
//高亮
[button2 setTitle:@"我被点中了" forState:UIControlStateHighlighted];
//[button2.titleLabel setFont:[UIFont systemFontOfSize:5]];
//位置(深一点知识)
//top:到顶部距离 left到左边距离 botton;到底部距离 right:到右边的距离
UIEdgeInsets edgeInset = UIEdgeInsetsMake(0, 0, 5, 5);
[button2 setTitleEdgeInsets:edgeInset];
//
// [button2.titleLabel setTextAlignment:NSTextAlignmentLeft];
button2.backgroundColor = [UIColor yellowColor];
//3.设置按钮的圆角 (深一点的知识)
//layer:层 它是按钮上面的一个属性, 可以通过设置layer,达到设置按钮圆角的目的
[button2.layer setMasksToBounds:YES];//是否可以设置圆角
[button2.layer setCornerRadius:10];//设置半径
[button2.layer setBorderWidth:5];//设置边框的宽度
//4.背景(会将整个的按钮填满)
button2.backgroundColor = [UIColor grayColor];
//图片的对象,保存图片
UIImage *image = [UIImage imageNamed:@"back.png"];
[button2 setBackgroundImage:image forState:UIControlStateNormal];
//5.设置图片
UIImage *image1 = [UIImage imageNamed:@"logo.png"];
[button2 setImage:image1 forState:UIControlStateNormal];
//显示
[self.window addSubview:button2];
//点击按钮触发事件
//target:响应方法的对象
//action:响应的方法
//UIControlEventTouchUpInside:点下抬起的时候
[button2 addTarget:self action:@selector(clickedButton:) forControlEvents:UIControlEventTouchUpInside];
//tag值 专门用来标记对应的按钮的
button1.tag = ButtonWithOne;
button2.tag = ButtonWithTwo;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
//传的参数默认就是调用这个方法的对象
-(void)clickedButton:(UIButton *)button{
//将按钮的状态变成选中
button.selected = YES;
NSLog(@"在点击方法里面:%i",button.tag);
if (button.tag == ButtonWithTwo) {
NSLog(@"第二个按钮");
//警告框 (代理部分:深一点知识)
/**
title:标题
message:显示的信息
delegate:nil
buttonTitle:按钮的题目 otherButtonTitles:是可以添加多个按钮
*/
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你可以进入下一个界面" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
//显示提示框
[alert show];
}else{
NSLog(@"第一个按钮");
}
}
#pragma mark alertViewDelegate方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
NSLog(@"buttonIndex == 0");
}else{
NSLog(@"buttonIndex == 1");
}
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end