您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  delegate