iOS 多视图—视图切换之代码块传参切换

在iOS设计中 ,视图在切换的时候同时能传参数到下一个视图页面的方法特别多,这里就以代码块实现传参的方法
 
 
 
FirstViewController.h

#import <UIKit/UIKit.h>

//声明代码块
typedef void (^PostValueBlock) (NSString *Info);
@interface FirstViewController : UIViewController<UITextFieldDelegate>


@property(strong,nonatomic)UITextField *textName;
@property(strong,nonatomic)NSString *stringB;
@property(strong,nonatomic)PostValueBlock Block;
@end
 
FirstViewController.m

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  //设置主屏背景色
    self.view.backgroundColor=[UIColor greenColor];
    //创建文本框
    self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 40)];
    self.textName.borderStyle=1;
    //指定代理
    self.textName.delegate=self;
    //传值
    self.textName.text=self.stringB;
    [self.view addSubview:self.textName];

   
}
//实现代码块方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
   //调用代码块方法
    if (self.Block) {
        self.Block(textField.text);
    }
    //切换页面
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"切换成功");
    }];
    //隐藏键盘
    if ([textField isFirstResponder]) {
        [textField resignFirstResponder];
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
 
 
@end
 
ViewController.h
 

#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface ViewController : UIViewController<UITextFieldDelegate>
@property(strong,nonatomic)NSString *stringA;
@property(strong,nonatomic)UIButton *myButton;
@property(strong,nonatomic)UITextField *textName;
@end
ViewController.m
 

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //背景色
    self.view.backgroundColor=[UIColor blueColor];
    //创建文本框
    self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 50)];
    self.textName.borderStyle=1;
    //指定代理
    self.textName.delegate=self;
    //传参
    self.textName.text=self.stringA ;//代码块
    [self.view addSubview:self.textName];

    //创建按钮
    self.myButton=[[UIButton alloc]initWithFrame:CGRectMake(150, 160, 50, 50)];
    self.myButton.backgroundColor=[UIColor redColor];
    [self.myButton setTitle:@"Next" forState:UIControlStateNormal];
    [self.myButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.myButton];
 
}
-(void)nextPage
{
    FirstViewController *firstVC=[[FirstViewController alloc]init];
    //传值到下一页面
   firstVC.stringB=self.textName.text;
    //实现代码块(传值到本页面)
    firstVC.Block=^(NSString *info){
        self.textName.text=info;
    };
    [self presentViewController:firstVC animated:YES completion:^{
       NSLog(@"切换成功");
   
    }];
   
}
//协议

//代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([textField isFirstResponder]) {
        [textField resignFirstResponder];
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
@end

 
效果图
1、从第一个视图传参(The only)到下一个视图(正向传参)
 
 
1、从视图传参(The only one of)到上一个视图(逆向传参)
 
 
 
posted @ 2016-03-15 19:45  右手指尖轻轻触  阅读(191)  评论(0编辑  收藏  举报