zoukankan      html  css  js  c++  java
  • iOS中使用block传值

    转自:http://blog.sina.com.cn/s/blog_60b45f230100yiaf.html

    用此方法传值可以替代委托了。具体例子:

    MainView.h
    #import <UIKit/UIKit.h>
    
    @interface MainView : UIViewController
    {
        IBOutlet UIButton* btn;
        IBOutlet UILabel* labShow;
    }
    -(IBAction)push:(id)sender;
    @end

    MainView.m

    #import "MainView.h"
    #import "SecondView.h"
    
    @implementation MainView
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
        }
        return self;
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
    -(IBAction)push:(id)sender
    {
        SecondView *s = [[SecondView alloc] initwithBlock:^(NSString *str){
            NSLog(@"%@",str);
            labShow.text = str;
        }];
        [self.navigationController pushViewController:s  animated:YES];
        [s release];
    }
    - (void)viewDidUnload
    {
        [super viewDidUnload];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    @end
    SecondView.h
    #import <UIKit/UIKit.h>
    typedef void (^MyBlock)(NSString *);
    
    @interface SecondView : UIViewController
    {
        IBOutlet UITextField* txtView;
        MyBlock my;
    }
    -(IBAction)back:(id)sender;
    -(id)initwithBlock:(MyBlock)str;
    @end
    SecondView.m
    #import "SecondView.h"
    
    @implementation SecondView
    
    -(id)initwithBlock:(MyBlock)str
    {
        self = [super init];
        if(self)
        {   
            my = str;
        }
        return self;
    }
    -(IBAction)back:(id)sender
    {
        NSString* s = txtView.text;
        if(my)
        {
            my(s);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }
    -(void)dealloc{
        Block_release(my);
        [super dealloc];
        
    }
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    @end

    界面:
    iOS中使用block传值

    iOS中使用block传值

    iOS中使用block传值

  • 相关阅读:
    洛谷 P3138 [USACO16FEB]Load Balancing S(二维前缀和,离散化)
    洛谷 P1052 [NOIP2005 提高组] 过河(dp,数学)
    洛谷 P1955 [NOI2015] 程序自动分析(并查集,离散化)
    洛谷 P3258 [JLOI2014]松鼠的新家(树上差分,lca)
    洛谷 P2296 [NOIP2014 提高组] 寻找道路(反图bfs)
    洛谷 P4141 消失之物(dp方案数)
    洛谷 P5322 [BJOI2019]排兵布阵(dp,分组背包)
    回溯算法
    分治法
    分支限界法
  • 原文地址:https://www.cnblogs.com/wangpei/p/3719284.html
Copyright © 2011-2022 走看看