iOS 页面与页面之间传参数的方法 代码块传值

Posted 唐唐_010

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 页面与页面之间传参数的方法 代码块传值相关的知识,希望对你有一定的参考价值。

代码块传值 是从后往前传值

1.声明代码块 (SecondXXX.h)

2.声明一个代码块类型的属性(SecondXXX.h)

3.调用代码块(SecondXXX.m)

4.实现代码块(SecondXXX.m)

 

#import <UIKit/UIKit.h>

#import "FirstViewController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

 @property (strong, nonatomic) UIWindow *window;

@end

 

#import "AppDelegate.h"

 

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    FirstViewController *first=[[FirstViewController alloc] init];

    self.window.rootViewController=first;

 

    return YES;

}

 

#import <UIKit/UIKit.h>

#import "SecondViewController.h"

@interface FirstViewController : UIViewController<UITextFieldDelegate>

 

@property(strong,nonatomic) UITextField *MyText;

@property(strong,nonatomic) UIButton *MyButton;

 

@end

 

#import "FirstViewController.h"

 

@interface FirstViewController ()

 

@end

 

@implementation FirstViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    

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

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

    self.MyText.borderStyle=1;

    self.MyText.delegate=self;

    self.MyText.tintColor=[UIColor redColor];

    [self.view addSubview:self.MyText];

    

    self.MyButton=[[UIButton alloc] initWithFrame:CGRectMake(150, 200, 100, 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

{

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

    //将文本框的内容赋给字符串

    second.MyStr=self.MyText.text;

    //4.实现代码块 显示信息

    second.MyBlock=^(NSString *Infor)

    {

        self.MyText.text=Infor;

    };

    //跳到下一页面

    [self presentViewController:second animated:YES completion:

     ^{

         NSLog(@"信息传递成功");

    }];

}

//隐藏键盘

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    if ([textField isFirstResponder])

    {

        [textField resignFirstResponder];

    }

    return YES;

}

 

#import <UIKit/UIKit.h>

//1.声明代码块 (SecondXXX.h)往前传值

typedef void(^PostValueBlock)(NSString *Infor);

 

#import "FirstViewController.h"

@interface SecondViewController : UIViewController<UITextFieldDelegate>

 

@property(strong,nonatomic) NSString *MyStr;

 

@property(strong,nonatomic) UITextField *MyText;

 

//2.声明一个代码块类型的属性(SecondXXX.h)

@property(strong,nonatomic) PostValueBlock MyBlock;

 

@end

 

 

#import "SecondViewController.h"

 

@interface SecondViewController ()

 

@end

 

@implementation SecondViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    //在父视图上添加背景图

    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"20130714160426_rPTnU.jpeg"]];

    //初始化文本框

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

    self.MyText.borderStyle=1;

    self.MyText.delegate=self;

    //将字符串赋给文本框

    self.MyText.text=self.MyStr;

    //在父视图上添加文本框

    [self.view addSubview:self.MyText];

}

 

//3. 调用代码块的方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    //3.1 调用Block

    if (self.MyBlock)

    {

        self.MyBlock(self.MyText.text);

    }

    //3.2 隐藏键盘

    if ([textField isFirstResponder])

    {

        [textField resignFirstResponder];

    }

    

    //3.3 返回上一页面

    [self dismissViewControllerAnimated:YES completion:

     ^{

         NSLog(@"上一页");

    }];

    

    return YES;

}

 

以上是关于iOS 页面与页面之间传参数的方法 代码块传值的主要内容,如果未能解决你的问题,请参考以下文章

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

iOS之页面传值

网页之间传值与获取值

前端页面之间传值--利用cookie存储全局变量

js实现页面与页面之间传值的几种方法优劣

微信小程序--如何在两个页面之间传值