iOS多界面傳值之--代理傳值

代理傳值

代理傳值一般用於逆向傳值,即第二個頁面傳值給第一個頁面
ViewController頁面push到SecondViewController頁面,如果SecondViewController頁面的信息想回傳(回調)到ViewController頁面,用代理傳值,其中SecondViewController定義協議和聲明代理,ViewController確認並實現代理,ViewController作爲SecondViewController的代理

  1. 第一步
  2. 首先新建一個SecondViewController,並且在SecondViewController裏面
  3. 定義協議
  4. 設置協議中的方法
  5. 聲明代理
  6. 第二步
  7. 在SecondViewController.m中爲其綁定相應方法
  8. 第三步
  9. ViewController.m中
  10. 設置代理
  11. 服從協議
  12. 實現代理方法

下面我們上代碼

1.在SecondViewController.h中創建協議方法

import <UIKit/UIKit.h>

//首先在SecondViewController.h中創建協議方法
@class SecondViewController;
//定義協議
@protocol PushValueDelegate <NSObject>

//設置協議中的方法
-(void)viewController:(SecondViewController *)ViewController didPushValueWithInfo:(id)info;


@end


@interface SecondViewController : UIViewController

//聲明代理
@property (nonatomic,strong)id<PushValueDelegate>delagate;

@end

2.在SecondViewController.m文件中

#import "SecondViewController.h"

@interface SecondViewController ()

@property (nonatomic,strong)UITextField *textField;
@property (nonatomic,strong)NSString *textString;

@end

@implementation SecondViewController

-(void)viewWillDisappear:(BOOL)animated{

//    設置視圖將要消失時.通過代理傳值
//    判斷代理是否存在,並且設置代理能夠響應代理方法時,才執行代理方法
    if (self.delagate && [self.delagate respondsToSelector:@selector(viewController:didPushValueWithInfo:)]) {

        [self.delagate viewController:self didPushValueWithInfo:_textField.text];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor cyanColor];
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textField];


}

3.回到我們的ViewController.m中,首先我們需要導入一下頭文件,然後在遵循在SecondViewController裏面聲明的協議,最後在viewDidLoad裏面創建UITextField 和 UIButton


#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()<PushValueDelegate>

@property (nonatomic,strong) UITextField *textField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"代理傳值";
    self.view.backgroundColor = [UIColor yellowColor];

    /**
      代理傳值(場景)一般用於逆向傳值,即第二個界面傳值給第一個界面
      ViewController頁面push到SecondViewController頁面,如果SecondViewController頁面的信息想回傳(回調)到ViewController頁面,用代理傳值,其中SecondViewController定義協議和聲明代理,ViewController確認並實現代理,ViewController作爲SecondViewController的代理

     第一步:首先,在SecondViewController.h中

     (1).定義協議

     (2).設置協議中的方法

     (3).聲明代理

     然後,在SecondViewController.m中

     (1).爲其綁定相應方法


     第二步:在FirstViewController.m中

     (1).設置代理

     (2).服從協議
     */


    _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.borderStyle = UITextBorderStyleRoundedRect;

    [self.view addSubview:_textField];


    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(150, 200, 80, 44);

    [btn setTitle:@"Next" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];


}

4.實現一下Button裏面的點擊事件方法,並設置代理

-(void)nextAction:(UIButton *)sender{

    SecondViewController *secondeVC = [[SecondViewController alloc]init];
//    設置代理
    secondeVC.delagate = self;

    [self.navigationController pushViewController:secondeVC animated:YES];


}

5.實現代理方法

//實現代理方法
-(void)viewController:(SecondViewController *)ViewController didPushValueWithInfo:(id)info{

    _textField.text = info;
}

6.由於是通過navigationController跳轉到SecondViewController裏面的,所以我們需要在AppDelegate裏面創建一個UINavigationController

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
    [self.window makeKeyAndVisible];


    return YES;
}

7.模擬器運行效果如下
這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章