代理和block反向传值

代理传值:

//  SendViewController.h
#import <UIKit/UIKit.h>
@protocol SendInFor <NSObject>
-(void)sendInForIdea:(NSString*)text;
@end
@protocol SendInForTwo <NSObject>
-(void)sender:(NSString*)text;
@end

@interface SendViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)id <SendInFor> delegate;
@property(nonatomic,retain)id <SendInForTwo> delegateB;
@property(nonatomic,retain)NSIndexPath*indexPath;

@e代理

代理方法

#import "SendViewController.h"

//代理传值
@interface SendViewController ()
{
    UITableView*table;
    NSMutableArray*data;
}
@end

@implementation SendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    data=[[NSMutableArray alloc]init];
    table=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    table.delegate=self;
    table.dataSource=self;
    [self.view addSubview:table];
    for (int i=0; i<20; i++) {

        NSString*string=[[NSString alloc]initWithFormat:@"数据%d",i];
        [data addObject:string];
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 60;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str=@"str";
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:str];
    if (cell==nil) {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
//    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    UIImageView *norImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    [norImage setImage:[UIImage imageNamed:@"image"]];

    cell.textLabel.text=[data objectAtIndex:indexPath.row];
    //点击单元格 变换背景图片
    if ([_indexPath isEqual: indexPath]) {

        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.backgroundColor=[UIColor redColor];
        cell.accessoryView=norImage ;

       /* 可以自定义图片  按钮 文本
        cell.accessoryType=btn;
        cell.accessoryType=label;
         */
    }
        else{
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.backgroundColor=[UIColor whiteColor];
        cell.accessoryView=nil ;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(sendInForIdea:)]) {

        _indexPath = indexPath;
        [self.delegate sendInForIdea:[data objectAtIndex:indexPath.row]];
        [table reloadData];

//      cell.imageView.image=[UIImage imageNamed:@"2"];
        //延迟操作     延迟跳转时间
       [self performSelector:@selector(pressBtn) withObject:nil afterDelay:3];

    //点击获取的 cell  cell变色
//    UITableViewCell *cell = [table cellForRowAtIndexPath:indexPath];
//    cell.backgroundColor=[UIColor cyanColor];
    }
}
-(void)pressBtn
{

 [self.navigationController popViewControllerAnimated:YES];

}

具体方法

block反向传值:

//  FirstViewController.h

#import <UIKit/UIKit.h>
 //直接回车,第一个参数是返回值类型,第二个为重定义的block名称,第三个为要传参数类型和参数名;然后需要定义重定义block类型的属性,并且实现参数为该重定义类型block的方法。
//typedef <#existing#> <#new#>;
//回调不加断点不容易找出错误
typedef void (^BlockColor)(UIColor *color);
typedef void (^BlockTitle)(NSString *title);

@interface FirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
//block设置为属性,修饰符用copy,即使使用strong,编译器也会将strong处理成copy
@property(nonatomic,copy)BlockColor color;
@property(nonatomic,copy)BlockTitle Title;

@endblock方法

block方法

//  FirstViewController.m

#import "FirstViewController.h"
#import "TableViewCell.h"

//block 反向传值
@interface FirstViewController ()
{
    UITableView *table;
    NSMutableArray *data;
    NSMutableArray *arr;
}
@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    arr =[[NSMutableArray alloc]init];
    for (int i=0; i<20; i++) {
        NSString * str=[[NSString alloc]initWithFormat:@"数据%d",i];
        [arr addObject:str];
    }

    table=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    table.dataSource=self;
    table.delegate=self;
    [self.view addSubview:table];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arr count];

}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *customCell = (TableViewCell *)cell;
    [UIView animateWithDuration:2 animations:^{
        customCell.image.alpha = 1.0f;
    }];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    TableViewCell *cell=[[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil] lastObject];
    cell.Title.text=[arr objectAtIndex:indexPath.row];

    //xib 中button不要设置点击事件   这样可以在cell里面调取按钮(点击单元格的按钮可以跳转)
    cell.button.tag=indexPath.row;
    [cell.button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.image.image =[UIImage imageNamed:@"2"];
    cell.image.alpha =0.2f;
    return cell;

}

- (void)clickAction:(UIButton *)sender
{
    NSInteger tag=sender.tag;
    NSLog(@"block 传值");
   // __weak typeof(self) weakSelf=self;
    if (_Title) {
        _Title([arr objectAtIndex:tag]);
    }
    if (_color) {
        _color([UIColor redColor]);
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"hello world");

   TableViewCell *cell = [table cellForRowAtIndexPath:indexPath];
    NSString *string = arr[indexPath.row];
    NSLog(@"string=%@",string);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

具体方法

调用:

//  RootViewController.h
#import <UIKit/UIKit.h>
#import "SendViewController.h"
@interface RootViewController : UIViewController<SendInFor>
#import "RootViewController.h"
#import "FirstViewController.h"
#import "SendViewController.h"
@interface RootViewController ()
{
    FirstViewController *first;
    SendViewController *send;
    UIButton *btn;
    UIButton *btn1;
}
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"test";
    first =[[FirstViewController alloc]init];
    send=[[SendViewController alloc]init];
    send.delegate=self;
    [self creatBtn];

}

-(void)creatBtn
{
    btn=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(50, 100, 100, 100)];
    [btn setBackgroundColor:[UIColor cyanColor]];
    [btn setTitle:@"block传值" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];

    btn1=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setFrame:CGRectMake(200, 100, 100, 100)];
    [btn1 setBackgroundColor:[UIColor cyanColor]];
    [btn1 setTitle:@"代理传值" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(pressBtn1) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [self.view addSubview:btn1];

}

-(void)pressBtn
{

    //int a = 0;   block 里面不能改变a的值(报错,block下面打印原来的值),但是可以将a的值赋给别人
    //弱引用属性
    //弱引用 self
    __block __weak typeof(self) weakSelf = self;
    __block __weak UIButton *_button = btn;
    first.color=^(UIColor *color){
    _button.backgroundColor=color;

    };

    first.Title=^(NSString *title){
    weakSelf.navigationItem.title=@"block传值";
    //强引用
   // [btn setTitle:title forState:UIControlStateNormal];
    //弱引用
    [_button setTitle:title forState:UIControlStateNormal];

    };
    [self.navigationController pushViewController:first animated:YES];
}

-(void)pressBtn1
{
    [self.navigationController pushViewController:send animated:YES];
}

-(void)sendInForIdea:(NSString *)text
{
    [btn1 setTitle:text forState:UIControlStateNormal];
    self.navigationItem.title=@"代理传值";
}

时间: 2024-12-25 04:50:20

代理和block反向传值的相关文章

代理和 block 传值的使用

// // ZYViewController.h // BlockTest // // Created by yejiong on 14/11/2. // Copyright © 2014年 zzz. All rights reserved. // #import <UIKit/UIKit.h> //1.声明一个 delegate 协议. @protocol ZYViewControllerDelegate <NSObject> //声明一个方法用来传值. //返回值表明传值的结果

ios开发的block反向传值

block 的反向传值,一直以来都是copy, 今天写出来用来加深印象, 也给一些懒哥们copy的方便些.不多说,直接上代码. #import <UIKit/UIKit.h> //第一步(第二个页面.h) typedef void (^secondVcBlock)(NSString*); @interface SecondViewController : UIViewController //第二步(第二个页面.h)声明一个属性 @property(nonatomic,copy)secondV

IOS Block 反向传值

1.在需要像上一个界面传值的.h 文件实现代理方法 @property (nonatomic, copy) void(^isOpenHandler)(BOOL) ; 2.在执行操作的时候需要江操作的结果反向传值给上个界面的时候调用Block if (self.isOpenHandler) { self.isOpenHandler(YES); } 3.在第一个视图控制器中 Push 的时候调用Block   接受回传回来的值 WM.isOpenHandler = ^(BOOL isopen){ i

IOS 代理和block的对比

一.block 方法从子视图向父视图传递值. 案例:使用block方式子视图向父视图传值.在子视图.h定义block,定义方法.在.m文件实例化方法.然后再主视图实例化子视图是使用block并传入相应的代码,在子视图点击的 addtarget下执行 父视图的传入的快代码.block这个流程清晰,比代理协议的跳跃性要小. .定义快代码 //在子视图的.h文件中 //定义快代码 typedef void(^textFieldChangedBlock)(NSString *text); - (id)i

使用代理和block写一个alertView

代理: MyAlertView.h: @property (nonatomic,assign)id delegate; @protocol MyAlertViewDelegate <NSObject> - (void)myAlertView:(MyAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; @end MyAlertView.m:- (void)buttonClick:(id)sender { UIButt

block反向传值

A.m B.h B.m

反向传值的几种常用方法

最近项目完成的差不多了,闲下来的时间突然心血来潮想自己写写以前没用过的方法.这里就包含了几种常见的反向传值的方法. 之所以现在专门自己写反向传值的博文记录,是因为之前几乎没怎么用到这些反向传值的方法,那么这里将会描述一下像"代理"."block"."通知"."单例"这四种传值方法. 首先是代理,也就是常看到的delegate.说的通俗点,就是委托一个对象,让其帮忙处理事情,说到底就是个中介的标志.详细方案如下: 在父类中调用其

反向传值实例

反向传值实例 1.代理反向传值 #import <UIKit/UIKit.h> //声明一个类 @class LHTableViewController; //声明一个协议 @protocol LHTableViewControllerDelegate <NSObject> //协议中的方法 -(void)LHTablieViewController:(LHTableViewController *)LHTablieViewController Color:(UIColor *)c

使用Nginx反向代理和proxy_cache缓存搭建CDN服务器加快Web访问速度

碰到问题: 移动用户访问web服务器www.osyunwei.com很慢 解决办法: 1.在移动机房放置一台nginx反向代理服务器 2.通过域名DNS智能解析,所有移动用户访问www.osyunwei.com时解析到nginx反向代理服务器 3.nginx反向代理服务器与web服务器之间采用专线连接 系统运维  www.osyunwei.com  温馨提醒:qihang01原创内容©版权所有,转载请注明出处及原文链接 说明: 1.web服务器 线路:电信 IP:192.168.21.129 域