iOS的执行周期

//
// XSNAppDelegate.m
// LessonComprehensive
//
// Created by 楠少轻狂 on 14-8-21.
// Copyright (c) 2014年 lanouhn. All rights reserved.
//

#import "XSNAppDelegate.h"

@interface XSNAppDelegate ()
{
UIView * _containerView;
}
@end

@implementation XSNAppDelegate


//当应用程序完成加载时触发,做一些程序加载之后的操作
- (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.

//创建背景视图
_containerView = [[UIView alloc] initWithFrame:self.window.bounds];
_containerView.backgroundColor = [UIColor cyanColor];
[self.window addSubview:_containerView];
[_containerView release];

//
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(60, 100, 200, 30)];
label.text = @"哈哈";
label.backgroundColor = [UIColor greenColor];
label.textAlignment = NSTextAlignmentCenter;
label.layer.cornerRadius = 5;
label.layer.masksToBounds = YES;
[_containerView addSubview:label];
[label release];

UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(60, 150, 200, 30)];
textField.placeholder = @"请输入手机号";
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.backgroundColor = [UIColor greenColor];
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.clearButtonMode = UITextFieldViewModeAlways;
textField.delegate = self;
textField.tag = 100;
[_containerView addSubview:textField];
[textField release];

UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(60, 200, 200, 30);
btn.backgroundColor = [UIColor greenColor];
btn.layer.cornerRadius = 5;
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:@"点我" forState:UIControlStateNormal];
[_containerView addSubview:btn];

UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
btn1.frame = CGRectMake(60, 250, 200, 30);
btn1.backgroundColor = [UIColor greenColor];
btn1.layer.cornerRadius = 5;
[btn1 addTarget:self action:@selector(cycle:) forControlEvents:UIControlEventTouchUpInside];
[btn1 setTitle:@"回收键盘" forState:UIControlStateNormal];
[_containerView addSubview:btn1];


// //将一个方法转换为字符串(笑笑语法)
// @"click:";
// //将一个字符串转换为方法
// SEL selector = NSSelectorFromString(@"click:");


self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

- (void)cycle:(UIButton *)btn
{
UITextField * tf = (UITextField *)[btn.superview viewWithTag:100];
[tf resignFirstResponder];
}

//点击按钮,判断输入框中的文字是否为11位,如果不是11位,提示给用户,输入错误
- (void)click:(UIButton *)btn
{
//1.获取输入框
UITextField * tf = (UITextField *)[btn.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:@"确定", @"Are you sure", nil];
alertView.alertViewStyle = UIAlertViewStyleDefault;
[alertView show];
[alertView release];
}
}

//UIAlertViewDelegate
//当点击button时会触发的方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//取消按钮的buttonIndex为0
//其他的按钮从左往右依次加1
NSLog(@"%d", buttonIndex);
switch (buttonIndex) {
case 0:
NSLog(@"取消按钮被点击");
break;
case 1:
NSLog(@"确定按钮被点击");
break;
case 2:
NSLog(@"Are you sure按钮被点击");
break;

default:
break;
}

}

//当点击键盘右下角的return按钮时触发
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"%s", __FUNCTION__);
[textField resignFirstResponder];
return YES;
}

//询问当前输入框能否被编辑,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
{
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)

//当应用程序将要取消活跃状态时触发(将要进入后台挂起)
- (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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值