码迷,mamicode.com
首页 > 其他好文 > 详细

Block传值原理

时间:2016-03-15 23:20:48      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

#import <UIKit/UIKit.h>

#import "SecondViewController.h"

@interface ViewController : UIViewController<UITextFieldDelegate>

@property (nonatomic ,strong) UITextField *textName;

@property (nonatomic ,strong) UIButton *button;

 

@end

 

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"5622630220130817130527093.jpg"]];

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

    self.textName.backgroundColor = [UIColor grayColor];

    self.textName.textColor = [UIColor greenColor];

    self.textName.delegate = self;

    [self.view addSubview:self.textName];

    self.button = [[UIButton alloc] initWithFrame:CGRectMake(120, 160, 80, 40)];

    [self.button setTitle:@"下一页" forState:UIControlStateNormal];

    [self.button addTarget:self action:@selector(nextPage) forControlEvents: UIControlEventTouchUpInside ];

    [self.view addSubview:self.button];

    

}

 

 

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    if ([self.textName isFirstResponder]) {

        [self.textName resignFirstResponder];

    }

    return YES;

}

 

 

 

-(void)nextPage

{

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

    second.str = self.textName.text;

    second.postvalueb = ^(NSString *str){

        self.textName.text = str;

        NSLog(@"%@",str);

    };

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

        NSLog(@"页面跳转成功");

    }];

    

    

}

 

 

#import <UIKit/UIKit.h>

typedef void(^postValueBlock)(NSString *);

 

@interface SecondViewController : UIViewController<UITextFieldDelegate>

@property (nonatomic ,strong) NSString *str;

@property (nonatomic ,strong) postValueBlock postvalueb;

@property (nonatomic ,strong)  UITextField *textFil;

@end

 

@implementation SecondViewController

 

- (void)viewDidLoad {

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"5622630220130817130527093.jpg"]];

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

    self.textFil.backgroundColor = [UIColor grayColor];

    self.textFil.textColor = [UIColor greenColor];

    //属性传值

    self.textFil.text = self.str;

    self.textFil.delegate = self;

    [self.view addSubview:self.textFil];

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    if(self.postvalueb){

               self.postvalueb(self.textFil.text);

        

    }

    if ([self.textFil isFirstResponder]) {

        [self.textFil resignFirstResponder];

    }

    [self dismissViewControllerAnimated:YES completion:nil];

    

    return YES;

}

 

 

@end

 

Block传值原理

标签:

原文地址:http://www.cnblogs.com/liumu/p/5281621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!