UI11_BLOCK传值

来源:互联网 发布:桌面小人软件 编辑:程序博客网 时间:2024/06/05 20:18
////  AppDelegate.h//  UI11_BLOCK传值////  Created by dllo on 15/8/12.//  Copyright (c) 2015年 flg. All rights reserved.//#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end
////  AppDelegate.m//  UI11_BLOCK传值////  Created by dllo on 15/8/12.//  Copyright (c) 2015年 flg. All rights reserved.//#import "AppDelegate.h"#import "MainViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (void)dealloc{    [_window release];    [super dealloc];}- (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 *naVC=[[UINavigationController alloc] initWithRootViewController:mainVC];    self.window.rootViewController=naVC;    [naVC release];    [mainVC release];    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.}- (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.}- (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.}- (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.}- (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end

////  MainViewController.h//  UI11_BLOCK传值////  Created by dllo on 15/8/12.//  Copyright (c) 2015年 flg. All rights reserved.//#import <UIKit/UIKit.h>@interface MainViewController : UIViewController@end

////  MainViewController.m//  UI11_BLOCK传值////  Created by dllo on 15/8/12.//  Copyright (c) 2015年 flg. All rights reserved.//#import "MainViewController.h"#import "secondViewController.h"@interface MainViewController ()<secondViewControllerdelegate>@end@implementation MainViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.navigationController.navigationBar.translucent=NO;    self.view.backgroundColor=[UIColor colorWithRed:255/255.0 green:212/255.0 blue:0 alpha:1];    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];    button.frame=CGRectMake(100, 100, 100, 30);    [button setTitle:@"下一页" forState:UIControlStateNormal];    [self.view addSubview:button];    button.layer.cornerRadius=10;    button.layer.masksToBounds=YES;    button.backgroundColor=[UIColor cyanColor];    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];}- (void)click:(UIButton *)button{    //没有参数,没有返回值的block    //通过block改变self.view的背景颜色//    void(^block)()=^(){//        NSLog(@"测试");//        //改变颜色//        self.view.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0  blue:arc4random()%256/255.0  alpha:1];//    };/////////    //调用//    block(); ///通过block从后向前传值    //block传值不需要返回值,,因为调用更需要返回值,所以传值时候只要参数,,不要返回值..//    void(^block)(NSString *)=^(NSString *str){///传过来的数据的处理都在block中进行...//        NSLog(@"%@",str);//    };    void(^block)(NSArray *)=^(NSArray *arr){        NSLog(@"%@",arr);    };    secondViewController *secondVC=[[secondViewController alloc] init];    [self.navigationController pushViewController:secondVC animated:YES];    [secondVC release];    //secondVC.delegate=self;    ///2.通过第二页属性接收block    secondVC.block=block;}-(void)red:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue{    self.view.backgroundColor=[UIColor colorWithRed:red green:green blue:blue alpha:1];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end

////  secondViewController.h//  UI11_BLOCK传值////  Created by dllo on 15/8/12.//  Copyright (c) 2015年 flg. All rights reserved.//#import <UIKit/UIKit.h>@protocol secondViewControllerdelegate<NSObject>- (void)red:(CGFloat )red      green:(CGFloat)green       blue:(CGFloat)blue;@end//@protocol secondViewControllerdelegate<NSObject>//@end@interface secondViewController : UIViewController@property(nonatomic,assign)id<secondViewControllerdelegate>delegate;//写一条属性 接受第一页向第二页传过来的block//@property(nonatomic,copy)void(^block)();//@property(nonatomic,copy)void(^block)(NSString *);@property(nonatomic,copy)void(^block)(NSArray *);@end

////  secondViewController.m//  UI11_BLOCK传值////  Created by dllo on 15/8/12.//  Copyright (c) 2015年 flg. All rights reserved.//#import "secondViewController.h"@interface secondViewController ()@end@implementation secondViewController//- (void)dealloc{//////}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor=[UIColor orangeColor];    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];    button.frame=CGRectMake(100, 100, 100, 30);    [button setTitle:@"返回" forState:UIControlStateNormal];    [self.view addSubview:button];    button.layer.cornerRadius=10;    button.layer.masksToBounds=YES;    button.backgroundColor=[UIColor cyanColor];    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];}-(void)click:(UIButton *)button{    [self.navigationController popViewControllerAnimated:YES];    [self.delegate red:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0];    ///3.调用传过来的block    //self.block();    //self.block(@"电视剧福克斯的建安费");    self.block(@[@"1",@"2",@"3"]);}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end


0 0