您好,登錄后才能下訂單哦!
怎樣實現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協議的具體操作,代碼詳細清楚,如果在日常工作遇到這個問題,希望你能通過這篇文章解決問題。如果想了解更多相關內容,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。