代碼塊傳值

#import<UIKit/UIKit.h>

#import "SecondViewController.h"

//遵循協(xié)議

@interface ViewController : UIViewController

@property(strong,nonatomic) UITextField *textName;

@property(strong,nonatomic) UIButton *btn;

@end


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

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

self.textName = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];

self.textName.borderStyle = 1;

[self.view addSubview:self.textName];

self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

self.btn.frame = CGRectMake(200, 200, 50, 50);

[self.btn setTitle:@"下一頁" forState:UIControlStateNormal];

[self.btn addTarget:self action:@selector(NextPage) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:self.btn];

}

////實現(xiàn)協(xié)議方法

//-(void)postValue:(NSString *)info

//{

//? ? self.textName.text = info;

//}

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

if ([textField isFirstResponder]) {

[textField resignFirstResponder];

}

return YES;

}

-(void)NextPage

{

SecondViewController *second = [[SecondViewController alloc] init];

second.str = self.textName.text;

//實現(xiàn)代碼塊

second.myblock = ^(NSString *info){

self.textName.text = info;

};

[self presentViewController:second animated:YES completion:^{

NSLog(@"Next");

}];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

/* 代碼塊傳值 從后往前傳

1.聲明代碼塊 (secondXXX.h)

2.聲明一個代碼塊類型的屬性 (SecondXXX.h)

3.調用代碼塊 (secondXXX.m)

4.實現(xiàn)代碼塊 (firstXXX.m)

*/

#import<UIKit/UIKit.h>

typedef void(^postValueBlock)(NSString *info);

@interface SecondViewController : UIViewController

@property(strong,nonatomic) NSString *str;

@property(strong,nonatomic) UITextField *textInfo;

@property(strong,nonatomic) postValueBlock myblock;

@end

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor grayColor];

NSLog(@"%@",self.str);

self.textInfo = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 100, 50)];

self.textInfo.borderStyle = 1;

self.textInfo.delegate = self;

self.textInfo.text = self.str;

[self.view addSubview:self.textInfo];

}

//3.調用代理方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

//1.調用block

if (self.myblock) {

self.myblock(textField.text);

}

//2.鍵盤隱藏

if ([textField isFirstResponder]) {

[textField resignFirstResponder];

}

//3.隱藏頁面

[self dismissViewControllerAnimated:YES completion:^{

NSLog(@"dismiss");

}];

return YES;

}

- (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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末样刷,一起剝皮案震驚了整個濱河市仑扑,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌颂斜,老刑警劉巖夫壁,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拾枣,死亡現(xiàn)場離奇詭異沃疮,居然都是意外死亡,警方通過查閱死者的電腦和手機梅肤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門司蔬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來万牺,“玉大人帆啃,你說我怎么就攤上這事∫厥保” “怎么了左医?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵授帕,是天一觀的道長。 經(jīng)常有香客問我浮梢,道長跛十,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任秕硝,我火速辦了婚禮芥映,結果婚禮上,老公的妹妹穿的比我還像新娘远豺。我一直安慰自己奈偏,他們只是感情好,可當我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布躯护。 她就那樣靜靜地躺著惊来,像睡著了一般。 火紅的嫁衣襯著肌膚如雪棺滞。 梳的紋絲不亂的頭發(fā)上裁蚁,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天内狸,我揣著相機與錄音,去河邊找鬼厘擂。 笑死昆淡,一個胖子當著我的面吹牛,可吹牛的內容都是我干的刽严。 我是一名探鬼主播昂灵,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼舞萄!你這毒婦竟也來了眨补?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤倒脓,失蹤者是張志新(化名)和其女友劉穎撑螺,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體崎弃,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡甘晤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了饲做。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片线婚。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖盆均,靈堂內的尸體忽然破棺而出塞弊,到底是詐尸還是另有隱情,我是刑警寧澤泪姨,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布游沿,位于F島的核電站,受9級特大地震影響肮砾,放射性物質發(fā)生泄漏诀黍。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一唇敞、第九天 我趴在偏房一處隱蔽的房頂上張望蔗草。 院中可真熱鬧,春花似錦疆柔、人聲如沸咒精。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽模叙。三九已至,卻和暖如春鞋屈,著一層夾襖步出監(jiān)牢的瞬間范咨,已是汗流浹背故觅。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留渠啊,地道東北人输吏。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像替蛉,于是被迫代替她去往敵國和親贯溅。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內容