iOS中的逆向传值问题

    xiaoxiao2021-04-14  35

    逆向传值

    下面我以TwoViewController向OneViewController传值为例为大家分享逆向传值的过程

    -代理传值

    OneViewController.m

    // // OneViewController.m // OC知识点整理 // // Created by li on 17/4/13. // Copyright © 2017年 李文强. All rights reserved. // #import "OneViewController.h" #import "TwoViewController.h" @interface OneViewController ()<TwoViewControllerDelegate> @end @implementation OneViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; [btn setTitle:@"跳转" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn]; } -(void)btn{ TwoViewController*vc=[[TwoViewController alloc]init]; //代理 vc.delegate=self; //跳转页面 [self presentModalViewController:vc animated:YES]; } //代理方法 -(void)TwoViewController:(NSString *)message{ NSLog(@"%@", message); } @end

    TwoViewController.h

    // // TwoViewController.h // OC知识点整理 // // Created by li on 17/4/13. // Copyright © 2017年 李文强. All rights reserved. // #import <UIKit/UIKit.h> //代理方法 @protocol TwoViewControllerDelegate <NSObject> @optional - (void)TwoViewController:(NSString *)message; @end @interface TwoViewController : UINavigationController //声明一个代理 @property (nonatomic,assign) id<TwoViewControllerDelegate>delegate; @end

    TwoViewController.m

    // // TwoViewController.m // OC知识点整理 // // Created by li on 17/4/13. // Copyright © 2017年 李文强. All rights reserved. // #import "TwoViewController.h" @interface TwoViewController () @end @implementation TwoViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; [btn setTitle:@"传值" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn]; } -(void)btn{ //为代理传值,这时才运行代理方法 [self.delegate TwoViewController:@"代理传值"]; //跳回原页面 [self dismissViewControllerAnimated:YES completion:NULL]; } @end

    -block传值

    OneViewController.m

    // // OneViewController.m // OC知识点整理 // // Created by li on 17/4/13. // Copyright © 2017年 李文强. All rights reserved. // #import "OneViewController.h" #import "TwoViewController.h" @interface OneViewController () @end @implementation OneViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; [btn setTitle:@"跳转" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn]; } -(void)btn{ TwoViewController*vc=[[TwoViewController alloc]init]; //block vc.block=^(NSString*text){ NSLog(@"%@",text); }; //跳转页面 [self presentModalViewController:vc animated:YES]; } @end

    TwoViewController.h

    // // TwoViewController.h // OC知识点整理 // // Created by li on 17/4/13. // Copyright © 2017年 李文强. All rights reserved. // #import <UIKit/UIKit.h> //block //重命名 typedef void (^ReturnTextBlock)(NSString *title); @interface TwoViewController : UINavigationController //声明一个block @property (nonatomic,copy)ReturnTextBlock block; @end

    TwoViewController.m

    // // TwoViewController.m // OC知识点整理 // // Created by li on 17/4/13. // Copyright © 2017年 李文强. All rights reserved. // #import "TwoViewController.h" @interface TwoViewController () @end @implementation TwoViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; [btn setTitle:@"传值" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn]; } -(void)btn{ //block if (_block) { _block(@"block传值"); } //跳回原页面 [self dismissViewControllerAnimated:YES completion:NULL]; } @end

    -通知传值

    OneViewController.m

    // // OneViewController.m // OC知识点整理 // // Created by li on 17/4/13. // Copyright © 2017年 李文强. All rights reserved. // #import "OneViewController.h" #import "TwoViewController.h" @interface OneViewController () @end @implementation OneViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; [btn setTitle:@"跳转" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn]; } -(void)btn{ TwoViewController*vc=[[TwoViewController alloc]init]; //接收通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealAction:) name:@"通知" object:nil]; //跳转页面 [self presentModalViewController:vc animated:YES]; } //通知响应 - (void)dealAction:(NSNotification *)sender { NSLog(@"%@",sender.object); } @end

    TwoViewController.m

    // // TwoViewController.m // OC知识点整理 // // Created by li on 17/4/13. // Copyright © 2017年 李文强. All rights reserved. // #import "TwoViewController.h" @interface TwoViewController () @end @implementation TwoViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; [btn setTitle:@"传值" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn]; } -(void)btn{ //发送通知 [[NSNotificationCenter defaultCenter] postNotificationName:@"通知" object:@"通知内容"]; //跳回原页面 [self dismissViewControllerAnimated:YES completion:NULL]; } @end
    转载请注明原文地址: https://ju.6miu.com/read-670422.html

    最新回复(0)