1. 程式人生 > >ios block傳值 最簡單的方法

ios block傳值 最簡單的方法

1、在您需要傳值出去的控制器標頭檔案裡宣告:

@property (nonatomic,copy) void(^stringblockr)(int age);

2、然後在你pop或者push到被傳值的頁面的方法裡,呼叫該方法:

self.stringblockr(15);

3、在被傳值的控制器中吊用你傳值頁面剛申明的方法:

SecondViewController *send = [[SecondViewControlleralloc]init];

    send.stringblockr = ^(int age) {

NSLog(@"%d",age);

    };


以下是整個流程:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (nonatomic,copy) void(^stringblockr)(int age);

@end

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {

    [superviewDidLoad];

self.view.backgroundColor = [UIColorpurpleColor];

UIButton *ba = [[UIButtonalloc]initWithFrame:CGRectMake(0, 0, 100,50)];

    ba.backgroundColor = [UIColorblueColor];

    [ba addTarget:selfaction:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];

    [self

.viewaddSubview:ba];

}

- (void)popAction {

self.stringblockr(15);

    [self.navigationControllerpopViewControllerAnimated:YES];

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

}

#import "ViewController.h"

#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.view.backgroundColor = [UIColorredColor];

}

- (IBAction)btnClick:(id)sender {

UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(30, 100, 100, 30)];

    [self.viewaddSubview:label];

    label.backgroundColor = [UIColororangeColor];

SecondViewController *send = [[SecondViewControlleralloc]init];

    send.stringblockr = ^(int age) {

NSLog(@"%d",age);

    };

    [self.navigationControllerpushViewController:send animated:YES];

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}



相關推薦

ios block 簡單方法

1、在您需要傳值出去的控制器標頭檔案裡宣告: @property (nonatomic,copy) void(^stringblockr)(int age); 2、然後在你pop或者push到被傳值的頁面的方法裡,呼叫該方法: self.stringblockr(

iOS Block、代理、通知中心

  Joker_King  關注 2016.04.18 22:17*  字數 802  閱讀 1792 評論 7 喜歡 12 在我們需

iOS block(逆

block是個好東西,是蘋果官方推薦的一種方法,效率高,程式碼簡單,今天用block主要是做一個簡單傳,暫時不對block詳細介紹,關於__block的用法,後期會一一介紹; 接下來,通過一個block,將 SecondViewController 的數值傳給  First

IOS Block

1.使用協議在下級像上級傳遞值得時候比較繁瑣,需要定義協議方法,定義屬性,呼叫,在上級裡設定代理實現代理方法。 2.使用block 在類前定義需要使用到的block, #import <UIKit/UIKit.h> /**  * 定義一個block 

iOS JS給OC方法(附HTML程式碼)

HTML程式碼<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <

【轉】使用git將項目上到github(簡單方法

名稱 posit gitignore nor this strong 共享 window mas 原文地址:http://www.cnblogs.com/cxk1995/p/5800196.html 首先你需要一個github賬號,所有還沒有的話先去註冊吧! https:/

使用git將專案上到github(簡單方法) - 轉

方法如下 https://www.cnblogs.com/cxk1995/p/5800196.html   唯一需要注意的地方: 紅線框起來的地方,一定要改成"first commit"   或者其他內容,例如 git commit  

git的簡單 使用git將專案上到github(簡單方法

  使用git將專案上傳到github(最簡單方法)   首先你需要一個github賬號,所有還沒有的話先去註冊吧! https://github.com/ 我們使用git需要先安裝git工具,這裡給出下載

使用git將專案上到github(簡單方法

首先你需要一個github賬號,所有還沒有的話先去註冊吧! https://github.com/ 我們使用git需要先安裝git工具,這裡給出下載地址,下載後一路直接安裝即可: https://git-for-windows.github.io/ 1.進入Git

Django實現任意檔案上簡單方法

利用Django實現檔案上傳並且儲存到指定路徑下,其實並不困難,完全不需要用到django的forms,也不需要django的models,就可以實現,下面開始實現。 第一步:在模板檔案中,建立一個form表單,需要特別注意的是,在有檔案上傳的form表單中,method屬

git上原生代碼到github(簡單方法,不用配置ssh)

今天幫同學上傳github程式碼時發現網上github入門教程都要配置ssh,很少有不需要配置ssh的,可是ssh配置好麻煩,這篇教程可以給不想配置ssh然後想使用github的使用者一點幫助。 第一步:建立Github新賬戶下載mygit 在gith

使用 git 將專案上到 github(簡單方法

首先你需要一個 github 賬號,所有還沒有的話先去註冊吧! 我們使用 git 需要先安裝 git 工具,這裡給出下載地址,下載後一路直接安裝即可: 1. 進入 Github 首頁,點選 New repository 新建一

iOS利用Block

ViewController.h 需要實現 #import <UIKit/UIKit.h> @interface ViewController : UIViewController - (void)sendMessage:(void(^)(NSSt

簡單方法將專案上到github

準備材料: 1.首先你需要一個github賬號,所有還沒有的話先去註冊吧!https://github.com/ 2.我們使用git需要先安裝git工具,這裡給出下載地址,下載後一路直接安裝即可:https://git-for-windows.github.

IOS簡單方法實現Cell文字高度自適應.

本教程適合新手. 系統:IOS7之後. 純文字高度. 血的教訓: 相信大家曾經為了實現cell的高度自適應,肯定少苦。那個時候,我是在我的資料模型model中新增一個height屬性,實現它的get方法,在get方法中計算需要多行顯示文字的高度,然後再加上其他亂七八糟的高度並返回,然

iOS-OC-iOS大全(代理,block ,單例,通知,屬性)

1、代理傳值 有A \B 兩個頁面需要將B頁面的值傳個A 總結: 1.首先應該在b頁面寫一個協議,協議裡有帶引數的方法,並建立實現該協議的屬性delegate,然後判斷該delegate_ 是否實現了協議的方法,實現該協議方法,並傳入值。 2.在A.h

使用git將項目上到github(簡單方法

就會 接下來 this src name 操作 生成文件 選擇 gin 首先你需要一個github賬號,所有還沒有的話先去註冊吧! https://github.com/ 我們使用git需要先安裝git工具,這裏給出下載地址,下載後一路直接安裝即可: https://git

Block 反向簡單應用

6.27  Block  用於反向傳值  在下一個頁面定義一個 block 塊 typedef void (^ReturnNickText)(NSString *NickText); @property (nonatomic, copy) ReturnNickText

iOS Block的基本使用以及Block

block為我們提供了一個非常便捷的方法去實現各種傳值以及回撥 合理的使用block可以減少程式碼量以及更加優雅的實現功能 現做個小整理如下: #pragma mark About Block //block的原型: NSString *(^myB

Java對數組的操作(三)—比較兩個數組中的元素是否同樣的簡單方法

con data println test popu pan equal main spa 呵呵呵,實現Java比較兩個數組中的元素是否同樣的功能你是怎麽做的?看以下最簡單方法: import java.util.Arrays;