block

//

//  AppDelegate.h

//  UI16_Block

//

//  Created by NinGery on 15/5/19.

//  Copyright (c) 2015 mac. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;



@end



//

//  AppDelegate.m

//  UI16_Block

//

//  Created by NinGery on 15/5/19.

//  Copyright (c) 2015 mac. All rights reserved.

//


#import "AppDelegate.h"

#import "MainViewController.h"

#import "SecondViewController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    [_window release];

    MainViewController *mainVC = [[MainViewController alloc] init];

    UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:mainVC];

    self.window.rootViewController = naviVC;

    

    [naviVC release];

    [mainVC release];

    

    

    return YES;

}


- (void)dealloc

{

    [_window release];

    [super dealloc];

}

@end





//

//  MainViewController.h

//  UI16_Block

//

//  Created by NinGery on 15/5/19.

//  Copyright (c) 2015 mac. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface MainViewController : UIViewController


@end




//

//  MainViewController.m

//  UI16_Block

//

//  Created by NinGery on 15/5/19.

//  Copyright (c) 2015 mac. All rights reserved.

//


#import "MainViewController.h"

#import "SecondViewController.h"

#import "NetHandler.h"


@interface MainViewController ()



@end


@implementation MainViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(30, 200, 300, 50);

    button.backgroundColor = [UIColor redColor];

    [button setTitle:@"Click" forState:UIControlStateNormal];

    [self.view addSubview:button];

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    

    

    [NetHandler getDataWithURLStr:@"http://api.douban.com/v2/movie/nowplaying?app_name=doubanmovie&client=e:iPhone4,1|y:iPhoneOS_6.1|s:mobile|f:doubanmovie_2|v:3.3.1|m:PP_market|udid:aa1b815b8a4d1e961347304e74b9f9593d95e1c5&alt=json&version=2&start=0&city=北京&apikey=0df993c66c0c636e29ecbb5344252a4a" completion:^(id result) {

       

        // result这个地方已经是字典或者数组了, 直接进行赋值或者转为数据模型对象即可

        NSLog(@"%@", result);

    }];

    

    

}




- (void)buttonAction:(UIButton *)btn

{

    

    // block语法

    

    // 1. 无返回值 无参数的block

    

    void(^block1)(void) = ^(void){

        

        

        NSLog(@"变黑");

        

    };

    

    // block的使用

    block1();

    

    // 2. 无返回值 有参数

    

    void(^block2)(NSString *str) = ^(NSString *str){

        


        // 改变BUTTON的标题

        [btn setTitle:str forState:UIControlStateNormal];

        

//        NSLog(@"张家伟是%@", str);

        

    };

    

//    block2(@"大水杯");

    

    

    

    

    // 3. 有返回值

    

    NSString *(^block3)(void) = ^(void){

        

        return @"返回值";

        

    };

    NSString *result = block3();

    NSLog(@"%@", result);

    

    

    

    

    

    SecondViewController *secondVC = [[SecondViewController alloc] init];

    

    secondVC.name = btn.currentTitle;

    

    // block给第二页赋值

    secondVC.block = block2;

    

    [self.navigationController pushViewController:secondVC animated:NO];

    [secondVC release];

}

@end






//

//  SecondViewController.h

//  UI16_Block

//

//  Created by NinGery on 15/5/19.

//  Copyright (c) 2015 mac. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController



@property (nonatomic, retain) NSString *name;



@property (nonatomic, copy) void(^block)(NSString *str);


@end


//

//  SecondViewController.m

//  UI16_Block

//

//  Created by NinGery on 15/5/19.

//  Copyright (c) 2015 mac. All rights reserved.

//


#import "SecondViewController.h"


@interface SecondViewController ()


@property (nonatomic, retain) UITextField *textField;



@end


@implementation SecondViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    

    

     self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 335, 50)];

    [self.view addSubview:self.textField];

    self.textField.borderStyle = UITextBorderStyleRoundedRect;

    

    self.textField.text = self.name;

    

    [self.textField release];

    

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(20, 300, 335, 50);

    [button setTitle:@"back" forState:UIControlStateNormal];

    button.backgroundColor = [UIColor cyanColor];

    [self.view addSubview:button];

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    

    

}


- (void)buttonAction:(UIButton *)btn

{

    self.block(self.textField.text);

    

    [self.navigationController popToRootViewControllerAnimated:YES];

}


- (void)dealloc

{

    [_textField release];

    [_name release];

    

    // 释放block变量

    Block_release(_block);

    

    

    [super dealloc];

}


@end






//

//  NetHandler.h

//  UI16_Block

//

//  Created by NinGery on 15/5/19.

//  Copyright (c) 2015 mac. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface NetHandler : NSObject


+ (void)getDataWithURLStr:(NSString *)str completion:(void(^)(id result))block;




@end





//

//  NetHandler.m

//  UI16_Block

//

//  Created by NinGery on 15/5/19.

//  Copyright (c) 2015 mac. All rights reserved.

//


#import "NetHandler.h"

#import <UIKit/UIKit.h>


@implementation NetHandler




+ (void)getDataWithURLStr:(NSString *)str completion:(void(^)(id result))block

{

    

    // get请求方式

    

    NSString *tempStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:tempStr];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    request.HTTPMethod = @"GET";

    

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        if (data != nil) {

        

        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        

        // result就是最后想要得到的结果, 使用block进行进一步数据操作.

        block(result);

            

        } else {

            

            NSLog(@"网络状况凄惨, 请检查");

//            UIAlertView

            

        }

        

    }];

    

}


@end



/*

 

 

 关于封装

 

 1.封装的时机: 一旦想对一段代码进行复制粘贴, 就是可以封装的标志; 自己写的代码非常 nb, 想保存下来给别人使用

 2.封装的目的: 减少代码的重复, 让代码可读性增强, 整体更加简洁

 3.封装的形式:

 

 如果一段代码只在当前的类中被(可能)反复调用, 就在当前类里面写一个方法封装.

 如果一段代码可以在很多不用的类中使用, 就把这些代码放到一个单独的类中,              一般情况下, 这个自定义的类都继承于NSObject/ UiView.

 一种情况: 代码和自定义的类的对象密切相关(UIView), 这些代码要放在减号方法中. 如果没有关系, 就放到加号方法中.

 

 如果一段代码只是对系统的类进行反复操作, 那就给这个系统类写一个类目.

 

 4.封装要注意的:

 

 管理好内存

 不要过度封装.


 */



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值