怎样实现Delegate协议?

2023-05-10,,

怎样实现Delegate协议?相信很多新手小白还没学会这个技能,通过这篇文章的总结,希望你能学会。如下资料是关于实现Delegate协议的步骤。

main.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[])
{
    //app的启动流程
    //1.一个iOS程序的启动,在main函数开始
    @autoreleasepool {
        
        //应用程序的启动函数  作用:1,启动一个时间循环,确保程序一直在执行 2.创建一个应用程序对象,3.指定一个代理人,去响应程序的各种状态
        //参数3:建立一个应用程序对象要使用的类,UIApplication是默认
        //参数4:指定一个类为应用程序的代理人,负责常用应用程序的各种状态
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

AppDelegate.h

#import <UIKit/UIKit.h>
//1,签订协议
@interface AppDelegate : UIResponder <UIApplicationDelegate,UIAlertViewDelegate, UITextFieldDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

AppDelegate.m

#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"当程序加载完成,调用这个方法") ;
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(80, 60, 150, 30)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = @"输入您想要的内容";
    textField.layer.borderWidth = 2;
    textField.layer.cornerRadius = 5;
    textField.clearButtonMode = UITextFieldViewModeAlways;
    
    //2.设置代理人
    textField.delegate = self;
    [self.window addSubview:textField];
    [textField release ];
 
    
    UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(80, 25, 150, 30)];
    textField1.borderStyle =UITextBorderStyleRoundedRect;
    textField1.placeholder = @"输入密码";
    textField1.layer.borderWidth = 1;
    textField1.layer.cornerRadius = 5;
    textField1.clearButtonMode = UITextFieldViewModeAlways;
    textField1.secureTextEntry = YES;
    //这里签订协议。来显示提示信息框框阿狂框框框框框框框框
    textField1.delegate = self;
    [self.window addSubview:textField1];
    [textField1 release];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"点击" forState:UIControlStateNormal];
    button.layer.cornerRadius = 5;
    [button setShowsTouchWhenHighlighted: YES];
    button.frame = CGRectMake(56, 100, 100, 30);
    button.alpha = 0.3;
    button.backgroundColor = [UIColor yellowColor];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:button];
    
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setTitle:@"你点我啊" forState:UIControlStateNormal];
    button1.layer.cornerRadius = 5;
    [button1 setShowsTouchWhenHighlighted:YES];
    button1.frame = CGRectMake(180, 100, 100, 30);
    button1.alpha = 0.3;
    button1.backgroundColor = [UIColor magentaColor];
    [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:button1];
    
    [_window release];
    return YES;
}
//是否能编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //能不能开始编辑状态
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"付费通知" message:@"如果您继续,需要收取$0.99" delegate:self cancelButtonTitle:@"就不交钱" otherButtonTitles:@"交钱", nil];
    [alertView show];
    [alertView release];
    return YES;
}
//对键盘return进行处理,这里的操作是对键盘回收 。。。。。。。。。
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //当点击键盘return键的时候
    [textField resignFirstResponder];
   
    return YES;
}
//这里控制键盘的输入,是否可以输入 ,这里控制键盘不能输入a
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //判断当前用户输入的字符
    if ([string isEqualToString:@"a"]) {
        return NO;
    }
    NSLog(@"%@", string);
    return YES;  // 如果是返回NO输入的都不显示
}
- (void)buttonClicked:(UIButton *)button
{
    
    //2。指定view的代理人  ,一般来说都是指定当前类的一个对象
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil];
    [alertView show];
    [alertView release];
}
//3.实现协议方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    //当点击alertview的某个按钮时,
    NSLog(@"点击按钮%d", buttonIndex);
    if (0 == buttonIndex) {
        NSLog(@"取消");
    }else if(1 == buttonIndex){
        NSLog(@"确定");
    }
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    
    //尽可能多的存储用户数据和当前的应用状态
    NSLog(@"当应用程序取消激活状态的时候调用");
    // 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
{
    NSLog(@"当程序进入后台之后调用");
    // 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
{
    //1.恢复应用进入后台之前的状态,撤销没有进行万完毕的操作。。。
    NSLog(@"应用程序即将进入前台,即将显示");
    // 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
{
    // 重新启动被昂听的各种任务/游戏,,可以在这个方法中刷新UI界面
    NSLog(@"应用程序处于激活状态的时候");
    // 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

以上就是实现Delegate协议的具体操作,代码详细清楚,如果在日常工作遇到这个问题,希望你能通过这篇文章解决问题。如果想了解更多相关内容,欢迎关注本站行业资讯频道!

《怎样实现Delegate协议?.doc》

下载本文的Word格式文档,以方便收藏与打印。