第一个简单的IOS程序是实现了一个QQ登陆界面

main:

复制代码
 1 #import <UIKit/UIKit.h>  2   3 #import "DXWAppDelegate.h"  4   5 int main(int argc, char *argv[])  6 {  7     //消息循环,当程序退出的时候才会y有返回值  8     @autoreleasepool {  9         //UIApplication是单粒的,是一个应用程序的象征,一个IOS程序对应一个UIApplication 10         //第三个参数用来指定Application类名(或者为子类),默认为nil(UIApplication类名),第四个参数用来指定代理类 11         return UIApplicationMain(argc, argv, nil, NSStringFromClass([DXWAppDelegate class])); 12         //return UIApplicationMain(argc, argv, NSStringFromClass([UIApplication class]), NSStringFromClass([DXWAppDelegate class])); 13  14     } 15 }
复制代码

//监听应用程序一些生命周期状态

AppDelegate.h

复制代码
 1 #import <UIKit/UIKit.h>  2   3 @class DXWViewController;  4   5 @interface DXWAppDelegate : UIResponder <UIApplicationDelegate>  6 //string暂时理解成retain,需要释放  7 @property (strong, nonatomic) UIWindow *window;  8   9 @property (strong, nonatomic) DXWViewController *viewController; 10  11 @end
复制代码

AppDelegate.m

复制代码
 1 //  2 //  DXWAppDelegate.m  3 //  firstDemo  4 //  5 //  Created by dingxiaowei on 13-5-20.  6 //  Copyright (c) 2013年 dingxiaowei. All rights reserved.  7 //  8   9 #import "DXWAppDelegate.h" 10  11 #import "DXWViewController.h" 12  13 @implementation DXWAppDelegate 14  15 - (void)dealloc 16 { 17     [_window release]; 18     [_viewController release]; 19     [super dealloc]; 20 } 21 #pragma mark - 应用程序加载完毕之后调用 22 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 23 { 24     NSLog(@"didFinishLaunchingWithOptions--程序加载完毕"); 25     //初始化一个窗口 26     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 27  28  29     //自己手动创建一个按钮 30 //    CGRect btnFrame=CGRectMake(100, 360, 100, 50); 31 //    UIButton *btn=[[UIButton alloc] initWithFrame:btnFrame];  //也可以这样  //UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame=btnFrame; 32 //    [btn setTitle:@"我是按钮" forState:UIControlStateNormal]; 33 //    [self.window addSubview:btn]; 34  35  36     // Override point for customization after application launch. 37     //传入一个xib文件名来初始化一个控制器 38     self.viewController = [[[DXWViewController alloc] initWithNibName:@"DXWViewController" bundle:nil] autorelease]; 39     self.window.rootViewController = self.viewController; 40     //上面的代码内部相当于执行了下面这行代码 41     //[self.window addSubview:self.viewController.view]; 42     //设置窗口可见   在众多窗口中keyWindow才能充当主窗口  只有主窗口才能跟用户交互 43     [self.window makeKeyAndVisible]; 44     //下面这个方法也能显示主界面,但是不能成为主窗口,也就是说不能跟用户打交到 45     //self.window.hidden=NO; 46     return YES; 47 } 48 #pragma mark - 程序失去焦点时候调用(指不能跟用户进行交互了) 49 - (void)applicationWillResignActive:(UIApplication *)application 50 { 51     NSLog(@"applicationWillResignActive--程序失去焦点了"); 52     // 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. 53     // 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. 54 } 55 #pragma mark - 当程序进入后台的时候,就调用这个方法 (比如点击home键) 56 - (void)applicationDidEnterBackground:(UIApplication *)application 57 { 58     NSLog(@"applicationDidEnterBackground--程序进入后台"); 59     // 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.  60     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 61 } 62 #pragma mark - 当程序进入前台的时候,调用该方法 63 - (void)applicationWillEnterForeground:(UIApplication *)application 64 { 65     NSLog(@"applicationWillEnterForeground--程序进入前台"); 66     // 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. 67 } 68 #pragma mark - 当应用程序获取焦点的时候调用 69 //获取焦点之后才可以跟用户进行交互 70 - (void)applicationDidBecomeActive:(UIApplication *)application 71 { 72     NSLog(@"applicationDidBecomeActive--获取焦点"); 73     // 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. 74 } 75 #pragma mark - 程序在某些情况下被终结是调用 76 - (void)applicationWillTerminate:(UIApplication *)application 77 { 78     NSLog(@"applicationWillTerminate--程序被关闭"); 79     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 80 } 81  82 @end
复制代码
ViewController.h
复制代码
 1 #import <UIKit/UIKit.h>  2   3 @interface DXWViewController : UIViewController  4 //UI元素不需要我们去进行内存管理  5 @property(nonatomic,assign) IBOutlet UITextField *qq;  6 @property(nonatomic,assign) IBOutlet UITextField *pwd;  7   8 //IBActoin其实就是void,但是它能够让方法名显示在xib的右击列表中  9 -(IBAction) login; 10  11 @end
复制代码

ViewController.m

复制代码
 1 #pragma mark - 登陆  2 -(void)login{  3     NSString *qqText=_qq.text;  //这里的下划线也就代表xib中的文本框  4     NSString *pwdText=_pwd.text;  5     NSLog(@"QQ=%@,密码=%@",qqText,pwdText);  6     //暂时理解:叫出键盘的那个视图就是第一响应者[FirstResponder]  7     //resignFirstResponder代表这个视图不想当第一相应者,那不想当以后键盘就退出  8     //退出键盘  9     //[_qq resignFirstResponder]; 10     //[_pwd resignFirstResponder]; 11     //或者这种方法(条件:如果第一响应者存在于当前调用着的view中,则能退出第一相应者) 12     [self.view endEditing:YES]; 13 } 14  15 - (void)viewDidLoad 16 { 17     [super viewDidLoad]; 18     // Do any additional setup after loading the view, typically from a nib. 19 } 20  21 - (void)didReceiveMemoryWarning 22 { 23     [super didReceiveMemoryWarning]; 24     // Dispose of any resources that can be recreated. 25 } 26  27 @end
复制代码