ui view怎么创建在微信左上角关闭按钮的叉号按钮,点击叉号

用block改写UIButton点击事件跟UIAlerView的按钮点击代理方法 - Iphone当前位置:& &&&用block改写UIButton点击事件跟UIAlerView的按钮点用block改写UIButton点击事件跟UIAlerView的按钮点击代理方法&&网友分享于:&&浏览:0次用block改写UIButton点击事件和UIAlerView的按钮点击代理方法1.用block改写UIButton点击事件
在这里给给出两种方式.
(1)自定义BlockButton,在初始化的时候给出按钮的样式(自定义)
自定义一个BlockButton继承UIButton,然后在里面用
addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
这个方法触发block.
MyBlockButton.h
1 #import &UIKit/UIKit.h&
3 @class MyBlockB
// 此处一定要声明
4 typedef void(^TouchBlock)(MyBlockButton *button);
6 @interface MyBlockButton : UIButton
8 @property (copy, nonatomic) TouchB
MyBlockButton.m
1 #import "MyBlockButton.h"
3 @implementation MyBlockButton
5 -(instancetype)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self) {
// 按钮边框美化
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor lightGrayColor].CGC
self.layer.cornerRadius = 3;
self.layer.masksToBounds = YES;
// 为按钮添加阴影
self.layer.shadowColor = [UIColor blackColor].CGC
self.layer.shadowOffset = CGSizeMake(3, 3);
self.layer.shadowRadius = 3;
// 调用此方法以触发block
[self addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
27 - (void)clickAction:(MyBlockButton *)button
_block(button);
ViewController.h里面没有添加任何代码
ViewController.m
1 #import "ViewController.h"
2 #import "MyBlockButton.h"
4 @interface ViewController ()
8 @implementation ViewController
10 - (void)viewDidLoad {
[super viewDidLoad];
// 创建一个"确定按钮"
MyBlockButton *button = [[MyBlockButton alloc]initWithFrame:CGRectMake(35, 100, 260, 30)];
[button setTitle:@"确定" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor orangeColor];
[button setBlock:^(MyBlockButton *blockButton) {
NSLog(@"按钮被点击了");
[self.view addSubview:button];
// 创建三个数字按钮
for (int i = 1; i &= 3; i ++) {
MyBlockButton *btn = [[MyBlockButton alloc]initWithFrame:CGRectMake(70 * i, 200, 60, 30)];
[btn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor redColor];
btn.block = ^(MyBlockButton *blockBtn) {
NSLog(@"按钮%d被点击了",i);
[self.view addSubview:btn];
37 - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
其运行效果如下:
(2)接下来给出的方法中,使用自定义方法调用继承自UIButton,在初始化使用的时候还需要设置其样式.
MyButtonBlock.h
1 #import &UIKit/UIKit.h&
3 typedef void(^TouchBlock)(UIButton *button);
5 @interface MyButtonBlock : UIButton
7 @property (copy, nonatomic) TouchB
9 // 自定义方法,调用block
10 - (void)clikAction:(TouchBlock)
MyButtonBlock.m
1 #import "MyButtonBlock.h"
3 @implementation MyButtonBlock
5 - (void)clikAction:(TouchBlock)block
// 调用此方法以触发block
[self addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
12 - (void)clickButton:(UIButton *)button
_block(button);
ViewController.h里面没有添加任何代码
ViewController.m
1 #import "ViewController.h"
2 #import "MyButtonBlock.h"
4 @interface ViewController ()
8 @implementation ViewController
10 - (void)viewDidLoad {
[super viewDidLoad];
MyButtonBlock *buttonBlock = [MyButtonBlock buttonWithType:UIButtonTypeCustom];
buttonBlock.frame = CGRectMake(35, 100, 300, 30);
[buttonBlock setTitle:@"确定" forState:UIControlStateNormal];
[buttonBlock setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
buttonBlock.backgroundColor = [UIColor orangeColor];
[buttonBlock clikAction:^(UIButton *button) {
NSLog(@"按钮被点击了!");
[self.view addSubview:buttonBlock];
26 - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
其运行效果如下:
2.用block改写UIButton点击事件和UIAlerView的代理
下面再改写Alert这个控件,思路与改写UIButton的点击事件的第二种方法是一样的,在自定义的Alert里面用block触发点击事件,而在Alert定义的代码里执行事件触发的行为.
MyAlertViewBlock.h
1 #import &UIKit/UIKit.h&
3 typedef void(^TouchBlock)(NSInteger buttonIndex);
5 @interface MyAlertViewBlock : UIAlertView
7 @property (copy, nonatomic) TouchB
9 // 自定义初始化方法
10 - (instancetype)initWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles
andTouchBlock:(TouchBlock)
MyAlertViewBlock.m
1 #import "MyAlertViewBlock.h"
3 @implementation MyAlertViewBlock
5 - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles andTouchBlock:(TouchBlock)block
// 先初始化父类的方法
self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
if (self) {
self.block =
15 #pragma mark - UIAlertViewDelegate(这只是一个提示,不需要签订代理协议)
16 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
_block(buttonIndex);
ViewController.h里面没有添加任何代码
ViewController.m
1 #import "ViewController.h"
2 #import "MyAlertViewBlock.h"
4 @interface ViewController ()
8 @implementation ViewController
10 - (void)viewDidLoad {
[super viewDidLoad];
// 创建一个UIButton(用于弹出UIAlertView)
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(35, 100, 260, 30);
button.backgroundColor = [UIColor orangeColor];
[button setTitle:@"确定" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
23 // 按钮点击方法
24 - (void)clickAction:(UIButton *)button {
MyAlertViewBlock *alertView = [[MyAlertViewBlock alloc]initWithTitle:@"测试"message:@"将UIAlertView的按钮点击事件代理用block实现" cancelButtonTitle:@"取消"otherButtonTitles:@"确定"andTouchBlock:^(NSInteger buttonIndex) {
//在这里面执行触发的行为,省掉了代理,这样的好处是在使用多个Alert的时候可以明确定义各自触发的行为,不需要在代理方法里判断是哪个Alert了
if (buttonIndex == 0) {
NSLog(@"取消");
} else if (buttonIndex == 1) {
NSLog(@"确定");
[alertView show];
其运行效果如下:
12345678910
12345678910
12345678910 上一篇:没有了下一篇:文章评论相关解决方案 1234567891011 Copyright & &&版权所有1053人阅读
iOS开发入门总结(1)
&&&&&& 学习iOS开发已经有一段时日了,之前一直没有系统的对iOS开发的相关知识进行归纳总结,导致很多知识点云里雾里在脑子里形不成iOS开发的思想,现将自己在学习过程中遇到的一些知识进行总结,希望能对iOS初学者能有一定的帮助。最初学iOS的时候苦于没有大神指点,全靠自己一点点摸索,确实走了很多弯路,不希望还有小伙伴跟我一样走过多的弯路。
  由于本人只是从去年11月份才开始玩iOS(附上自己的学习路线,如下图),受限于能力,难免有一些不完善或不恰当的地方,希望大神们多多见谅,勿拍砖,有不足或需要完善的地方也希望小伙伴们能多多指教。
  作为iOS开发的入门总结的第一篇,这里以最常见的注册或登陆页面设计作为开篇,这里主要会用到UILabel,UIButton,UITextField这三个控件。为了让繁琐的知识点介绍不至于太枯燥泛味,我采用“总--&分”的方式来进行讲解,即先介绍结果,让大家提起兴趣来,然后再针对结果中遇到的各个知识点来分别进行讲解。这样读者看起来就可以各取所取,不需要的地方就可以直接跳过,节省时间。
一.注册或登录界面
& & & 如下图,实现了一个注册或登录界面,这里先只介绍单个页面,不介绍点击“完成”按钮后的动作,在之后的系列《iOS开发入门总结》中会逐步介绍,等基础知识介绍的差不多了,就可以以一个完整的注册系统作为小结。
&& & 说明:
这里为了展现UITextField的文本框关联键盘的设置,这里把“密码”和“确定密码”的关联键盘都设置为数字键盘,实际应用中密码一般都允许为数字或字母。实现了键盘收回操作。这里没有写对“用户名”进行特殊字符过滤的代码。
&& 实现代码:
#import &ViewController.h&
@interface ViewController ()
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//三个UILabel
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, 80, 37)];
nameLabel.font = [UIFont systemFontOfSize:15];
nameLabel.text = @&用
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.textAlignment = NSTextAlignmentL
nameLabel.numberOfLines = 2;
//用于设置UILabel中文本的行数
[self.view addSubview:nameLabel];
[nameLabel release];
UILabel *newPasswordLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 60+40, 80, 37)];
newPasswordLabel.font = [UIFont systemFontOfSize:15];
newPasswordLabel.text = @&密
newPasswordLabel.backgroundColor = [UIColor clearColor];
newPasswordLabel.textAlignment = NSTextAlignmentL
[self.view addSubview:newPasswordLabel];
[newPasswordLabel release];
UILabel *oncePasswordLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 60+40*2, 80, 37)];
oncePasswordLabel.font = [UIFont systemFontOfSize:15];
oncePasswordLabel.text = @&确认密码:&;
oncePasswordLabel.backgroundColor = [UIColor clearColor];
oncePasswordLabel.textAlignment = NSTextAlignmentL
[self.view addSubview:oncePasswordLabel];
[oncePasswordLabel release];
//三个输入框
UITextField *nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 60, 210, 30)];
nameTextField.placeholder = @&请输入用户名&;
nameTextField.tag = 1;
[nameTextField setSecureTextEntry:NO];
nameTextField.font = [UIFont systemFontOfSize:14];
nameTextField.delegate =
nameTextField.backgroundColor = [UIColor clearColor];
nameTextField.borderStyle = UITextBorderStyleRoundedR
[self.view addSubview:nameTextField];
[nameTextField release];
UITextField *passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 60+40, 210, 30)];
passwordTextField.placeholder = @&至少6位数字&;
passwordTextField.tag = 2;
[passwordTextField setSecureTextEntry:YES];
passwordTextField.font = [UIFont systemFontOfSize:14];
passwordTextField.delegate =
passwordTextField.backgroundColor = [UIColor clearColor];
passwordTextField.borderStyle = UITextBorderStyleRoundedR
passwordTextField.keyboardType = UIKeyboardTypeNumberP
[self.view addSubview:passwordTextField];
[passwordTextField release];
UITextField *onceNewPasswordTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 60+40*2, 210, 30)];
onceNewPasswordTextField.placeholder = @&请再次输入密码&;
onceNewPasswordTextField.tag = 3;
onceNewPasswordTextField.font = [UIFont systemFontOfSize:14];
[onceNewPasswordTextField setSecureTextEntry:YES];
onceNewPasswordTextField.delegate =
onceNewPasswordTextField.backgroundColor = [UIColor clearColor];
onceNewPasswordTextField.borderStyle = UITextBorderStyleRoundedR
onceNewPasswordTextField.keyboardType = UIKeyboardTypeNumberP
[self.view addSubview:onceNewPasswordTextField];
[onceNewPasswordTextField release];
UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
confirmButton.frame = CGRectMake(110, 60+40*3+20, 100, 37);
[confirmButton setTitle:@&确定& forState:UIControlStateNormal];
//正常状况下button显示的标题
[confirmButton setTitle:@&确定& forState:UIControlStateHighlighted];
//高亮显示时button的标题
confirmButton.backgroundColor = [UIColor redColor];
[confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];//button被按下又抬起后发生的事件
//@selector可以理解为&选择子&,selector是一个指针变量,类似于sender。 这里是将method的方法指定给新建的这个confirmButton
[self.view addSubview:confirmButton];
//收回键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
for (int i = 0; i&4; i++) {
UITextField *textField = (UITextField*)[self.view viewWithTag:1+i];
[textField resignFirstResponder];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
二.UILabel
  UILabel继承了UIView,它可以设置UIView所支持的属性。
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)];
//设置Label的位置和大小
//设置显示文字
label1.text = @&用户名&;
//设置字体:粗体,正常的是 SystemFontOfSize
label1.font = [UIFont boldSystemFontOfSize:20];
//设置文字颜色
label1.textColor = [UIColor orangeColor];
//设置文字位置
label1.textAlignment = UITextAlignmentR
label2.textAlignment = UITextAlignmentC
//设置字体大小适应label宽度
label4.adjustsFontSizeToFitWidth = YES;
//设置label的行数
label5.numberOfLines = 2;
UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色
//设置高亮
label6.highlighted = YES;
label6.highlightedTextColor = [UIColor orangeColor];
//设置阴影
label7.shadowColor = [UIColor redColor];
label7.shadowOffset = CGSizeMake(1.0,1.0);
//设置是否能与用户进行交互
label7.userInteractionEnabled = YES;
//设置label中的文字是否可变,默认值是YES
label3.enabled = NO;
//设置文字过长时的显示格式
label3.lineBreakMode = UILineBreakModeMiddleT//截去中间
typedef enum {
UILineBreakModeWordWrap = 0,
UILineBreakModeCharacterWrap,
UILineBreakModeClip,//截去多余部分
UILineBreakModeHeadTruncation,//截去头部
UILineBreakModeTailTruncation,//截去尾部
UILineBreakModeMiddleTruncation,//截去中间
} UILineBreakM
//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为
label4.baselineAdjustment = UIBaselineAdjustmentN
typedef enum {
UIBaselineAdjustmentAlignBaselines,
UIBaselineAdjustmentAlignCenters,
UIBaselineAdjustmentNone,
} UIBaselineA
有时需要设置UILabel中文本的行数,其属性值默认为1,用于设置该UILabel只能显示一行文本。
oldPasswordLabel.numberOfLines = <span style="color:#;
三.UITextField
//初始化textfield并设置位置及大小
UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];
//当输入框没有内容时,水印提示 ,提示内容为“用户名”
//显示灰色字体,作为提示信息
text.placeholder = @&用户名&;
//设置边框样式,只有设置了才会显示边框样式
text.borderStyle = UITextBorderStyleRoundedR
typedef enum {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
} UITextBorderS
//设置键盘的样式
text.keyboardType = UIKeyboardTypeNumberP
typedef enum {
UIKeyboardTypeDefault,
//默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 
//支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 
//标准电话键盘,支持+*#字符
UIKeyboardTypeURL,
//URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad,
//数字键盘
UIKeyboardTypePhonePad,   
//电话键盘
UIKeyboardTypeNamePhonePad,  
//电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress,  
//用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad,
//数字键盘 有数字和小数点
UIKeyboardTypeTwitter,
//优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardT
//每输入一个字符就变成点 用语密码输入
text.secureTextEntry = YES;
//设置输入框的背景颜色,此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉
text.backgroundColor = [UIColor whiteColor];
//设置背景图片
text.background = [UIImage imageNamed:@&dd.png&];
//设置背景
text.disabledBackground = [UIImage imageNamed:@&cc.png&];
//设置输入框内容的字体样式和大小
text.font = [UIFont fontWithName:@&Arial& size:20.0f];
//设置字体颜色
text.textColor = [UIColor redColor];
//输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容
text.clearButtonMode = UITextFieldViewModeA
typedef enum {
UITextFieldViewModeNever, 
//从不出现
UITextFieldViewModeWhileEditing,
//编辑时出现
UITextFieldViewModeUnlessEditing, 
//除了编辑外都出现
UITextFieldViewModeAlways  
//一直出现
} UITextFieldViewM
//输入框中一开始就有的文字
text.text = @&一开始就在输入框的文字&;
//是否纠错
text.autocorrectionType = UITextAutocorrectionTypeNo;
typedef enum {
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo,  
//不自动纠错
UITextAutocorrectionTypeYes, 
//自动纠错
} UITextAutocorrectionT
//再次编辑就清空
text.clearsOnBeginEditing = YES;
//内容对齐方式
text.textAlignment = UITextAlignmentL
//内容的垂直对齐方式
UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment
text.contentVerticalAlignment = UIControlContentVerticalAlignmentC
//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
textFied.adjustsFontSizeToFitWidth = YES;
//设置自动缩小显示的最小字体大小
text.minimumFontSize = 20;
//首字母是否大写
text.autocapitalizationType = UITextAutocapitalizationTypeN
typedef enum {
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
} UITextAutocapitalizationT
//return键变成什么键
text.returnKeyType =UIReturnKeyD
typedef enum {
UIReturnKeyDefault,
//默认 灰色按钮,标有Return
UIReturnKeyGo,
//标有Go的蓝色按钮
UIReturnKeyGoogle,
//标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin,
//标有Join的蓝色按钮
UIReturnKeyNext,
//标有Next的蓝色按钮
UIReturnKeyRoute,
//标有Route的蓝色按钮
UIReturnKeySearch,
//标有Search的蓝色按钮
UIReturnKeySend,
//标有Send的蓝色按钮
UIReturnKeyYahoo,
//标有Yahoo的蓝色按钮
UIReturnKeyYahoo,
//标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall,
//紧急呼叫按钮
} UIReturnKeyT
//键盘外观
textView.keyboardAppearance=UIKeyboardAppearanceDefault;
typedef enum {
UIKeyboardAppearanceDefault,
//默认外观,浅灰色
UIKeyboardAppearanceAlert,   //深灰 石墨色
} UIReturnKeyT
//设置代理 用于实现协议
text.delegate =
//把textfield加到视图中
[self.window addSubview:text];
//最右侧加图片是以下代码  左侧类似
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@&right.png&]];
text.rightView=
text.rightViewMode = UITextFieldViewModeA
typedef enum {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
} UITextFieldViewM
在处理密码等隐私类的信息时,可能需要将输入的信息隐藏一下。
//每输入一个字符就变成点 ,用语密码输入
[passwordTextField setSecureTextEntry:YES];
  也可以设置文本框关联的键盘,如下:
//设置键盘的样式
text.keyboardType = UIKeyboardTypeNumberP
typedef enum {
UIKeyboardTypeDefault,
//默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 
//支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 
//标准电话键盘,支持+*#字符
UIKeyboardTypeURL,
//URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad,
//数字键盘
UIKeyboardTypePhonePad,   
//电话键盘
UIKeyboardTypeNamePhonePad,  
//电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress,  
//用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad,
//数字键盘 有数字和小数点
UIKeyboardTypeTwitter,
//优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardT
有时需要限制输入文本的长度,这类操作也非常普遍和重要。
//限制输入文本的长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
if ([textField.text length] & MAXLENGTH)
textField.text = [textField.text substringToIndex:MAXLENGTH-1];
return NO;
return YES;
四.UIButton
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
能够定义的button类型有以下6种,
typedef enum {
UIButtonTypeCustom = 0,
自定义风格
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
蓝色小箭头按钮,主要做详细说明用
UIButtonTypeInfoLight,
亮色感叹号
UIButtonTypeInfoDark,
暗色感叹号
UIButtonTypeContactAdd,
十字加号按钮
} UIButtonT
//给定button在view上的位置
button1.frame = CGRectMake(20, 20, 280, 20);
//button背景色
button1.backgroundColor = [UIColor clearColor];
//设置button填充图片
//[button1 setImage:[UIImage imageNamed:@&btng.png&] forState:UIControlStateNormal];
//设置button标题
[button1 setTitle:@&点击& forState:UIControlStateNormal];
/* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
//以下是几种状态
UIControlStateNormal
常规状态显现
UIControlStateHighlighted
高亮状态显现
UIControlStateDisabled
禁用的状态才会显现
UIControlStateSelected
UIControlStateApplication
= 0x00FF0000, 当应用程序标志时
UIControlStateReserved
= 0xFF000000
为内部框架预留,可以不管他
* 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
* 那么可以去掉这个功能
button1.adjustsImageWhenHighlighted = NO;
/*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
button1.adjustsImageWhenDisabled = NO;
/* 下面的这个属性设置为yes的状态下,按钮按下会发光*/
button1.showsTouchWhenHighlighted = YES;
/* 给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是
按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。
触发了这个事件以后,执行butClick:这个方法,addTarget:self 的意思是说,这个方法在本类中
也可以传入其他类的指针*/
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
//显示控件
[self.view addSubview:button1];
单独说明一下:
UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
confirmButton.frame = CGRectMake(110, 60+40*3+20, 100, 37);
[confirmButton setTitle:@&确定& forState:UIControlStateNormal];
//正常状况下button显示的标题
[confirmButton setTitle:@&确定& forState:UIControlStateHighlighted];
//高亮显示时button的标题
confirmButton.backgroundColor = [UIColor redColor];
[confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];//button被按下又抬起后发生的事件
//@selector可以理解为&选择子&,selector是一个指针变量,类似于sender。 这里是将method的方法指定给新建的这个confirmButton
[self.view addSubview:confirmButton];
若要设置UIButton的背景图片时:
UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
confirmButton.frame = CGRectMake(10, 60, 100, 40);
UIImage *nextStepImage = [UIImage imageNamed:@&app.png&];
UIImage *nextStepDownImage = [UIImage imageNamed:@&app.png&];
nextStepImage = [nextStepImage resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
nextStepDownImage = [nextStepDownImage resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
[confirmButton setBackgroundImage:nextStepImage forState:UIControlStateNormal];
[confirmButton setBackgroundImage:nextStepDownImage forState:UIControlStateHighlighted];
[confirmButton setTitle:@&确定& forState:UIControlStateNormal];
[confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:confirmButton];
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:62767次
积分:1322
积分:1322
排名:千里之外
原创:62篇
转载:14篇
评论:15条
(1)(2)(1)(28)(12)(32)

我要回帖

更多关于 微信左上角返回按钮 的文章

 

随机推荐