你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS程序執行過程

iOS程序執行過程

編輯:IOS開發綜合
//UIApplication main函數是應用程序的入口, 做了:
        //1 創建應用程序對象(UIApplication對象)
        //2 指定應用程序的代理(通過代理來監測應用程序的執行狀態)
        //3 創建事件循環(死循環)
//
//  CCLAppDelegate.m
//  LessonComprehensive
//
//  Created by lanouhn on 14-8-21.
//  Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//

#import "CCLTestDelegte.h"

@interface CCLTestDelegte ()
{
    UIView *_bgView;
    UILabel *_lable;
    UITextField *_tf;
    UIButton *_button;
}
@end

@implementation CCLTestDelegte

//當應用程序加載完成之時觸發
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"%s", __FUNCTION__);
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    _bgView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:_bgView];
    [_bgView release];
    
    _lable = [[UILabel alloc] initWithFrame:CGRectMake(110, 50, 100, 40)];
    _lable.backgroundColor = [UIColor grayColor];
    _lable.layer.cornerRadius = 5;
    _lable.text = @"lable";
    _lable.layer.masksToBounds = YES;
    [_bgView addSubview:_lable];
    [_lable release];
    
    _tf = [[UITextField alloc] initWithFrame:CGRectMake(110, 110, 100, 40)];
    _tf.backgroundColor = [UIColor grayColor];
    _tf.placeholder = @"文本框";
    _tf.keyboardType = UIKeyboardTypeNumberPad;
    _tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    _tf.delegate = self;
    _tf.tag = 100;
    _tf.layer.cornerRadius = 5;
    [_bgView addSubview:_tf];
    [_tf release];
    
    _button = [UIButton buttonWithType:UIButtonTypeSystem];
    _button.frame = CGRectMake(110, 170, 100, 40);
    [_button setTitle:@"點我" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    _button.layer.cornerRadius = 5;
    _button.layer.masksToBounds = YES;
    _button.backgroundColor = [UIColor grayColor];
    [_bgView addSubview:_button];
    
    //    SEL sel = NSSelectorFromString(@"click:");將方法名字符串轉換為方法
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

//點擊按鈕判斷輸入框中的文字是否是11位, 若不是11為則提示用戶輸入錯誤
- (void)click:(UIButton *)button
{
    //1 獲取輸入框
    UITextField *tf = (UITextField *)[button.superview viewWithTag:100];
    //2 判斷輸入框文字的長度
    if ([tf.text length] != 11) {
        //        NSLog(@"手機號碼輸入錯誤");
        //title 提示框的標題 message 提示框的提示信息 delegate 代理 cancelButtonTitle 取消按鈕顯示的文字 otherButtonTitles 其他按鈕顯示的文字, 只給按鈕顯示的文字即可, 可以有多個
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"手機號輸入有誤" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"啊", @"確定", nil];
        //讓alert彈出
        //UIAlertViewStyleDefault = 0,
        //UIAlertViewStyleSecureTextInput,
        //UIAlertViewStylePlainTextInput,
        //UIAlertViewStyleLoginAndPasswordInput
        alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
        [alertView show];
        [alertView release];
    }
}

//當點擊Button時會觸發的方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //取消按鈕的buttonIndex為0, 其他的按鈕, 從上到下依次加1
    NSLog(@"buttonIndex %d", buttonIndex);
    
    //通過switch case來判斷哪個按鈕被點擊
    switch (buttonIndex) {
        case 0:
            NSLog(@"取消按鈕被點擊");
            break;
        case 1:
            NSLog(@"啊按鈕被點擊");
            break;
        case 2:
            NSLog(@"確定按鈕被點擊");
            break;
        default:
            break;
    }
}

//詢問當前輸入框能否開始編輯, yes能 no不能
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //輸出方法名
    NSLog(@"%s", __FUNCTION__);
    return YES;
}// return NO to disallow editing.


//當輸入框開始編輯時觸發(獲得焦點後觸發)
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"%s", __FUNCTION__);
}// became first responder

//詢問當前輸入框是否結束編輯(鍵盤是否收回)
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"%s", __FUNCTION__);
    return YES;
}// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end

//當前輸入框結束時觸發(鍵盤收回之後觸發)
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"%s", __FUNCTION__);
}// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called

//當前輸入框文字發生變化時觸發(只有通過鍵盤輸入時, 文字改變, 觸發)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length >= 11) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"手機號為11位" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:@"好", nil];
        [alertView show];
        alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
        [alertView release];
        
        textField.text = [textField.text substringToIndex:10];
    }
    NSLog(@"%s", __FUNCTION__);
    return YES;
}// return NO to not change text

//用來控制輸入框的清除按鈕是否具有清除功能(yes 有 no 沒有)
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    NSLog(@"%s", __FUNCTION__);
    return YES;
}// called when clear button pressed. return NO to ignore (no notifications)

//當點擊鍵盤右下角的return時觸發
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"%s", __FUNCTION__);
    [textField resignFirstResponder];
    return YES;
}// called when 'return' key pressed. return NO to ignore.

//當應用程序將要取消活躍狀態(將要進入後台是觸發)
- (void)applicationWillResignActive:(UIApplication *)application
{
    NSLog(@"%s", __FUNCTION__);
    // 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(@"%s", __FUNCTION__);
    // 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
{
    NSLog(@"%s", __FUNCTION__);
    // 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
{
    NSLog(@"%s", __FUNCTION__);
    // 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
{
    NSLog(@"%s", __FUNCTION__);
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


//當有電話進入時: applicationWillResignActive: 1 拒絕: 應用程序狀態: applicationDidBecomeActive: 2 接聽:applicationDidEnterBackground:
- (void)dealloc
{
    [_window release];
    [super dealloc];
}

@end

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved