第一阶段的总结0816
程序运行的生命周期
对象的消失和出现的过程
viewDidLoad调用的时机是self.view 的getter方法的调用后
根据UIApplicationMain函数,程序将进入AppDelegate.m,这个文件是xcode新建工程时自动生成的。下面看一下AppDelegate.m文件,这个关乎着应用程序的生命周期。
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
- // Override point for customization after application launch.
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- NSLog(@"iOS_didFinishLaunchingWithOptions");
- return YES;
- }
- - (void)applicationWillResignActive:(UIApplication *)application
- {
- // 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.
- NSLog(@"iOS_applicationWillResignActive");
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
- // 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.
- NSLog(@"iOS_applicationDidEnterBackground");
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- // 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.
- NSLog(@"iOS_applicationWillEnterForeground");
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- // 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.
- NSLog(@"iOS_applicationDidBecomeActive");
- }
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
- NSLog(@"iOS_applicationWillTerminate");
- }
1、application didFinishLaunchingWithOptions:当应用程序启动时执行,应用程序启动入口,只在应用程序启动时执行一次。若用户直接启动,lauchOptions内无数据,若通过其他方式启动应用,lauchOptions包含对应方式的内容。
2、applicationWillResignActive:在应用程序将要由活动状态切换到非活动状态时候,要执行的委托调用,如 按下 home 按钮,返回主屏幕,或全屏之间切换应用程序等。
3、applicationDidEnterBackground:在应用程序已进入后台程序时,要执行的委托调用。
4、applicationWillEnterForeground:在应用程序将要进入前台时(被激活),要执行的委托调用,刚好与applicationWillResignActive 方法相对应。
5、applicationDidBecomeActive:在应用程序已被激活后,要执行的委托调用,刚好与applicationDidEnterBackground 方法相对应。
6、applicationWillTerminate:在应用程序要完全推出的时候,要执行的委托调用,这个需要要设置UIApplicationExitsOnSuspend的键值。
初次启动:
2013-05-24 20:20:31.550 LifeIOS[451:c07] iOS_didFinishLaunchingWithOptions
2013-05-24 20:20:31.551 LifeIOS[451:c07] iOS_applicationDidBecomeActive
按下home键:
2013-05-24 20:22:17.349 LifeIOS[451:c07] iOS_applicationWillResignActive
2013-05-24 20:22:17.350 LifeIOS[451:c07] iOS_applicationDidEnterBackground
点击程序图标进入:
2013-05-24 20:22:56.913 LifeIOS[451:c07] iOS_applicationWillEnterForeground
2013-05-24 20:22:56.914 LifeIOS[451:c07] iOS_applicationDidBecomeActive
程序中没有设置UIApplicationExitsOnSuspend的值,程序不会彻底退出。
3.对象的之间的传值
对于这方面感觉,自己还比较清晰。主要是找到对象(在对象与对象之间碰面时是最好时机)
4.XML/HTML的解析
XML
NSString *str=@"<stu><stu1>stu1</stu1><stu2>age</stu2></stu>";
NSDictionary *dic=[NSDictionary dictionaryWithXMLString:str];
NSLog(@"%@",dic);
HTML解析
NSString *html=[[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://dict.youdao.com/search?q=link&keyfrom=dict.index"] encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@",html); HTMLParser *parser =[[HTMLParser alloc] initWithString:html error:nil]; HTMLNode *bNode=[parser body]; NSArray *node=[bNode findChildrenWithAttribute:@"class" matchingName:@"keyword" allowPartial:YES]; NSLog(@"==%@",node.description); for (HTMLNode *no in node) { NSLog(@"++%@",[no contents]); } NSArray *node1=[bNode findChildrenWithAttribute:@"class" matchingName:@"pronounce" allowPartial:YES]; for (HTMLNode *no1 in node1) { NSLog(@"+++%@",[no1 contents]); } NSError *error; NSString *html = @"<ul>" "<li><input type='image' name='input1' value='string1value' /></li>" "<li><input type='image' name='input2' value='string2value' /></li>" "</ul>" "<span class='spantext'><b>Hello World 1</b></span>" "<span class='spantext'><b>Hello World 2</b></span>"; HTMLParser *parser = [[HTMLParser alloc] initWithString:html error:&error]; if (error) { NSLog(@"Error: %@", error); return; } HTMLNode *bodyNode = [parser body]; NSArray *inputNodes = [bodyNode findChildTags:@"input"]; for (HTMLNode *inputNode in inputNodes) { if ([[inputNode getAttributeNamed:@"name"] isEqualToString:@"input2"]) { NSLog(@"-----%@", [inputNode getAttributeNamed:@"value"]); //Answer to first question } } NSArray *spanNodes = [bodyNode findChildTags:@"span"]; for (HTMLNode *spanNode in spanNodes) { if ([[spanNode getAttributeNamed:@"class"] isEqualToString:@"spantext"]) { NSLog(@"===%@", [spanNode rawContents]); //Answer to second question } }
代理的应用
#pragma mark - NSURLConnectionDataDelegate //4. 只有出错才会到这里 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"Failed: %@", error); } //1. 先接受到响应信息,能够知道等下接受的数据是什么样的 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"Response: %@", response); NSLog(@"Total length: %lld", [response expectedContentLength]); } //NSFileHandle //2. 分次接受数据 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { if (connection==connection1) { [_mData1 appendData:data]; } else { [_mData2 appendData:data]; } } - (void)getImage:(NSDictionary *)str imgView:(UIImageView *)imgView label:(UILabel*)lab { NSString *string=[NSString stringWithFormat:@"http://112.124.25.143:8080/TradeCms%@",str[@"imgUrl"]]; NSURL *url1=[NSURL URLWithString:string]; NSURLRequest *imageRequest=[NSURLRequest requestWithURL:url1 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; // NSLog(@"--@--%@",url1); [NSURLConnection sendAsynchronousRequest:imageRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // NSLog(@"%@",data); UIImage *image=[UIImage imageWithData:data]; imgView.image=image; lab.text=str[@"title"]; }]; // NSData *imageData=[NSURLConnection sendSynchronousRequest:imageRequest returningResponse:nil error:nil]; // NSData *imageData=[NSData dataWithContentsOfURL:url1 options:NSDataReadingMappedIfSafe error:nil]; // UIImage *image=[UIImage imageWithData:imageData]; // return image; } - (void)getImage:(NSDictionary *)str flag:(int)fg { NSString *string=[NSString stringWithFormat:@"http://112.124.25.143:8080/TradeCms%@",str[@"smallImg"]]; NSURL *url1=[NSURL URLWithString:string]; // NSLog(@"--@--%@",url1); NSURLRequest *imageRequest=[NSURLRequest requestWithURL:url1 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; // NSLog(@"--@--%@",url1); [NSURLConnection sendAsynchronousRequest:imageRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // NSLog(@"%@",data); flag++; NSString *key=[NSString stringWithFormat:@"%d",fg]; [imgDic setObject:data forKey:key]; // table.delegate=self; // table.dataSource=self; // [table reloadData]; if (flag>9) { table.delegate=self; table.dataSource=self; [table reloadData]; if ([activity isAnimating]) { [activity removeFromSuperview]; } } }]; } //3. 下载完成 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { if (connection==connection1) { NSLog(@"conn1 finished..."); NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:_mData1 options:NSJSONReadingAllowFragments error:nil]; // NSLog(@"%@",dic); _mArray1=[dic objectForKey:@"contents"]; // NSLog(@"%@",_mArray1); [self getImage:_mArray1[0]imgView:scrImageView1 label:xinXiLab]; [self getImage:_mArray1[1]imgView:scrImageView2 label:xinXiLab2]; [self getImage:_mArray1[2]imgView:scrImageView3 label:xinXiLab3]; // if ([activity isAnimating]) // { // [activity removeFromSuperview]; // } } else { NSLog(@"conn2 finished..."); NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:_mData2 options:NSJSONReadingAllowFragments error:nil]; // NSLog(@"%@",dic); _mArray2=[dic objectForKey:@"contents"]; // NSLog(@"%@",_mArray2); for (int i=0; i<10; i++) { [self getImage:_mArray2[i] flag:i]; } // table.delegate=self; // table.dataSource=self; // [table reloadData]; } }