iOS:使用block進行類與類,控制器與控制器之間的傳值

來源:互聯網
上載者:User

標籤:

介紹:在iOS中類與類,控制器與控制器之間傳值的方式有多種,分別是:單例傳值、代理傳值、通知傳值、block傳值或者一些喜好設定也可以用來傳值。。。。

每一種傳值方式都有各自的優點和缺點,針對不同情況選擇符合需要的方式進行傳值。這裡我比較喜歡block方式傳值,個人覺得簡單好用,省卻了不少代碼。

下面簡單給一個在控制器之間FirstViewController和SecondViewController使用block傳值的例子:

在SecondViewController中:

SecondViewController.h檔案


//
//  SecondViewController.h
//  block傳值
////  Created by mac on 16/6/16.//  Copyright © 2016年 JDYang. All rights reserved.//

#import <UIKit/UIKit.h>

/**
 *  定義一個block別名,方便使用,該block傳回值為void類型,有一個參數為字串類型
 */
typedef void (^ReturnValueByBlock)(NSString *text);


@interface SecondViewController : UIViewController
/**
 *  聲明一個block變數
 */
@property (copy,nonatomic)ReturnValueByBlock returnBlock;


/**
 *  聲明一個執行個體方法,block代碼塊作為參數
 */
-(void)returnValue:(ReturnValueByBlock) block;
@end

SecondViewController.m檔案


//
//  SecondViewController.m
//  block傳值
////  Created by mac on 16/6/16.//  Copyright © 2016年 JDYang. All rights reserved.//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //返回
    self.view.backgroundColor = [UIColor greenColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(back:)];
}

//返回
-(void)back:(UIBarButtonItem *)sender{
   
    [self.navigationController popViewControllerAnimated:YES];
        //給block參數賦值    self.returnBlock(@"我是賽亞人!");}

//通過執行個體方法回傳值
-(void)returnValue:(ReturnValueByBlock)block{
    self.returnBlock = block;
}
@end

 

在FirstViewController中:

FirstViewController.m檔案


//
//  ViewController.m
//  block傳值
////  Created by mac on 16/6/16.//  Copyright © 2016年 JDYang. All rights reserved.
//

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()
@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //建立按鈕
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    btn.center = self.view.center;
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"入棧" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

//入棧
-(void)push:(UIButton *)sender{

    SecondViewController *secondVC = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secondVC animated:YES];
   
    //接收回傳的值
    [secondVC returnValue:^(NSString *text) {
        NSLog(@"%@",text);
    }];
}
@end 

iOS:使用block進行類與類,控制器與控制器之間的傳值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.