delegate
2015-08-22 20:35
232 查看
#import "AppDelegate.h"
#warning 系统提供的协议使用: 1.给当前的类签订协议
@interface
AppDelegate ()<UITextFieldDelegate,
UIAlertViewDelegate>
@end
@implementation AppDelegate
- (void)dealloc
{
[_window
release];
[super
dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
self.window = [[UIWindow
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor
whiteColor];
[self.window
makeKeyAndVisible];
[_window
release];
UITextField *textField = [[UITextField
alloc] initWithFrame:CGRectMake(20,
20, 335,
40)];
textField.borderStyle =
UITextBorderStyleRoundedRect;
#warning 系统提供的协议使用: 2.指定textField的代理人,一般是self
textField.delegate =
self;
[self.window
addSubview:textField];
[textField
release];
/*
系统提供的协议的命名特征:
1. 协议的命名:
一般来说都是 提供协议的类名+Delegate/DataSource
2. 协议中方法的命名:
一般来说都是以 类名作为方法开头(去掉UI/NS...)
3. 协议方法中的关键字会反映当前方法的调用时机:
should: 是不是应该做...返回值一般是Bool
did: 已经做过了就调用这个方法,一般没有返回值
will: 将要开始做就调用这个方法,一般没有返回值
*/
UIButton *button = [UIButton
buttonWithType:UIButtonTypeSystem];
button.frame =
CGRectMake(20,
120, 335,
40);
<
da63
span style="color:#000000;"> [button setTitle:@"button"
forState:UIControlStateNormal];
button.backgroundColor = [UIColor
yellowColor];
[button addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.window
addSubview:button];
// NSTimer的使用
// NSTimer是iOS中的计时器类,每隔固定的秒数反复调用同一个方法
//
参数1: 时间间隔
//
参数2: 调用方法的对象
//
参数3: 调用的方法
//
参数4: 可以传一个参数到timer对象的userinfo属性
//
参数5: 是否重复
[NSTimer
scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(timerAction:)
userInfo:nil
repeats:YES];
return
YES;
}
- (void)timerAction:(NSTimer *)timer
{
NSLog(@"计时器");
}
- (void)buttonAction:(UIButton *)btn
{
UIAlertView *alertView = [[UIAlertView
alloc] initWithTitle:@"title"
message:@"message"
delegate:self
cancelButtonTitle:@"cancel"
otherButtonTitles:@"OK",
nil];
[alertView
show];
[alertView
release];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//
判断用户点击的是哪个按钮
NSLog(@"%ld",buttonIndex);
//
使用if或者switch对index做判断
if (buttonIndex ==
0) {
self.window.backgroundColor = [UIColor
redColor];
}else
if (buttonIndex ==
1){
self.window.backgroundColor = [UIColor
blackColor];
}
}
#warning 系统提供的协议使用: 3.根据需要实现协议方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"是否开始编辑状态");
return
YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"已经开始编辑");
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
NSLog(@"%@",string);
// NSLog(@"%@",NSStringFromRange(range));
//
屏蔽a
if ([string isEqualToString:@"a"]) {
return NO;
}
//
当在textField中输入字符的时候,系统会使用这个方法对每个字符做判断
return
YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//
使用这个协议方法回收键盘
[textField resignFirstResponder];//
回收键盘的方法
return
YES;
}
#warning 系统提供的协议使用: 1.给当前的类签订协议
@interface
AppDelegate ()<UITextFieldDelegate,
UIAlertViewDelegate>
@end
@implementation AppDelegate
- (void)dealloc
{
[_window
release];
[super
dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
self.window = [[UIWindow
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor
whiteColor];
[self.window
makeKeyAndVisible];
[_window
release];
UITextField *textField = [[UITextField
alloc] initWithFrame:CGRectMake(20,
20, 335,
40)];
textField.borderStyle =
UITextBorderStyleRoundedRect;
#warning 系统提供的协议使用: 2.指定textField的代理人,一般是self
textField.delegate =
self;
[self.window
addSubview:textField];
[textField
release];
/*
系统提供的协议的命名特征:
1. 协议的命名:
一般来说都是 提供协议的类名+Delegate/DataSource
2. 协议中方法的命名:
一般来说都是以 类名作为方法开头(去掉UI/NS...)
3. 协议方法中的关键字会反映当前方法的调用时机:
should: 是不是应该做...返回值一般是Bool
did: 已经做过了就调用这个方法,一般没有返回值
will: 将要开始做就调用这个方法,一般没有返回值
*/
UIButton *button = [UIButton
buttonWithType:UIButtonTypeSystem];
button.frame =
CGRectMake(20,
120, 335,
40);
<
da63
span style="color:#000000;"> [button setTitle:@"button"
forState:UIControlStateNormal];
button.backgroundColor = [UIColor
yellowColor];
[button addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.window
addSubview:button];
// NSTimer的使用
// NSTimer是iOS中的计时器类,每隔固定的秒数反复调用同一个方法
//
参数1: 时间间隔
//
参数2: 调用方法的对象
//
参数3: 调用的方法
//
参数4: 可以传一个参数到timer对象的userinfo属性
//
参数5: 是否重复
[NSTimer
scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(timerAction:)
userInfo:nil
repeats:YES];
return
YES;
}
- (void)timerAction:(NSTimer *)timer
{
NSLog(@"计时器");
}
- (void)buttonAction:(UIButton *)btn
{
UIAlertView *alertView = [[UIAlertView
alloc] initWithTitle:@"title"
message:@"message"
delegate:self
cancelButtonTitle:@"cancel"
otherButtonTitles:@"OK",
nil];
[alertView
show];
[alertView
release];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//
判断用户点击的是哪个按钮
NSLog(@"%ld",buttonIndex);
//
使用if或者switch对index做判断
if (buttonIndex ==
0) {
self.window.backgroundColor = [UIColor
redColor];
}else
if (buttonIndex ==
1){
self.window.backgroundColor = [UIColor
blackColor];
}
}
#warning 系统提供的协议使用: 3.根据需要实现协议方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"是否开始编辑状态");
return
YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"已经开始编辑");
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
NSLog(@"%@",string);
// NSLog(@"%@",NSStringFromRange(range));
//
屏蔽a
if ([string isEqualToString:@"a"]) {
return NO;
}
//
当在textField中输入字符的时候,系统会使用这个方法对每个字符做判断
return
YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//
使用这个协议方法回收键盘
[textField resignFirstResponder];//
回收键盘的方法
return
YES;
}
相关文章推荐
- C#使用委托(delegate)实现在两个form之间传递数据的方法
- Query中click(),bind(),live(),delegate()的区别
- jQuery中 delegate使用的问题
- 浅谈jquery中delegate()与live()
- 由点击页面其它地方隐藏div所想到的jQuery的delegate
- jQuery中delegate与on的用法与区别示例介绍
- JS delegate与live浅析
- jQuery事件 delegate()使用方法介绍
- .NET笔记之:Delegate内部原理的分析
- C#匿名方法与Delegate类型转换错误分析
- jQuery中delegate和on的用法与区别详细解析
- UITextField常用delegate
- 试说明在C#中Delegate类的使用
- delegate 委托的使用
- C#的Delegate的小心得
- iOS 如何选择delegate、notification、KVO?
- 代理传值和 block 回调的步骤。
- IOS 如何选择delegate、notification、KVO?
- AFNetworking使用
- 关于SearchaBar的bookMarks(代理方式实现)