您好,登錄后才能下訂單哦!
怎樣實現(xiàn)Delegate協(xié)議?相信很多新手小白還沒學(xué)會這個技能,通過這篇文章的總結(jié),希望你能學(xué)會。如下資料是關(guān)于實現(xiàn)Delegate協(xié)議的步驟。
main.h
#import <UIKit/UIKit.h> #import "AppDelegate.h" int main(int argc, char * argv[]) { //app的啟動流程 //1.一個iOS程序的啟動,在main函數(shù)開始 @autoreleasepool { //應(yīng)用程序的啟動函數(shù) 作用:1,啟動一個時間循環(huán),確保程序一直在執(zhí)行 2.創(chuàng)建一個應(yīng)用程序?qū)ο螅?.指定一個代理人,去響應(yīng)程序的各種狀態(tài) //參數(shù)3:建立一個應(yīng)用程序?qū)ο笠褂玫念?,UIApplication是默認(rèn) //參數(shù)4:指定一個類為應(yīng)用程序的代理人,負(fù)責(zé)常用應(yīng)用程序的各種狀態(tài) return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }
AppDelegate.h
#import <UIKit/UIKit.h> //1,簽訂協(xié)議 @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(@"當(dāng)程序加載完成,調(diào)用這個方法") ; 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 = @"輸入您想要的內(nèi)容"; textField.layer.borderWidth = 2; textField.layer.cornerRadius = 5; textField.clearButtonMode = UITextFieldViewModeAlways; //2.設(shè)置代理人 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; //這里簽訂協(xié)議。來顯示提示信息框框阿狂框框框框框框框框 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 { //能不能開始編輯狀態(tài) UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"付費(fèi)通知" message:@"如果您繼續(xù),需要收取$0.99" delegate:self cancelButtonTitle:@"就不交錢" otherButtonTitles:@"交錢", nil]; [alertView show]; [alertView release]; return YES; } //對鍵盤return進(jìn)行處理,這里的操作是對鍵盤回收 。。。。。。。。。 - (BOOL)textFieldShouldReturn:(UITextField *)textField { //當(dāng)點擊鍵盤return鍵的時候 [textField resignFirstResponder]; return YES; } //這里控制鍵盤的輸入,是否可以輸入 ,這里控制鍵盤不能輸入a - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { //判斷當(dāng)前用戶輸入的字符 if ([string isEqualToString:@"a"]) { return NO; } NSLog(@"%@", string); return YES; // 如果是返回NO輸入的都不顯示 } - (void)buttonClicked:(UIButton *)button { //2。指定view的代理人 ,一般來說都是指定當(dāng)前類的一個對象 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil]; [alertView show]; [alertView release]; } //3.實現(xiàn)協(xié)議方法 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; { //當(dāng)點擊alertview的某個按鈕時, NSLog(@"點擊按鈕%d", buttonIndex); if (0 == buttonIndex) { NSLog(@"取消"); }else if(1 == buttonIndex){ NSLog(@"確定"); } } - (void)dealloc { [_window release]; [super dealloc]; } - (void)applicationWillResignActive:(UIApplication *)application { //盡可能多的存儲用戶數(shù)據(jù)和當(dāng)前的應(yīng)用狀態(tài) NSLog(@"當(dāng)應(yīng)用程序取消激活狀態(tài)的時候調(diào)用"); // 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(@"當(dāng)程序進(jìn)入后臺之后調(diào)用"); // 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.恢復(fù)應(yīng)用進(jìn)入后臺之前的狀態(tài),撤銷沒有進(jìn)行萬完畢的操作。。。 NSLog(@"應(yīng)用程序即將進(jìn)入前臺,即將顯示"); // 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 { // 重新啟動被昂聽的各種任務(wù)/游戲,,可以在這個方法中刷新UI界面 NSLog(@"應(yīng)用程序處于激活狀態(tài)的時候"); // 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
以上就是實現(xiàn)Delegate協(xié)議的具體操作,代碼詳細(xì)清楚,如果在日常工作遇到這個問題,希望你能通過這篇文章解決問題。如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。