日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS传值之代理传值

發布時間:2025/3/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS传值之代理传值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

iOS中傳值方式有好幾種,分別是:代理傳值,block傳值,屬性傳值,通知傳值,單例傳值,利用userdefault或者文件方式傳值,通常代理傳值和block傳值使用最普遍,本文介紹代理傳值的方式,后續博客會一次寫上其他傳值方式。

一 什么是委托代理?

1、協議(protocol),就是使用了這個協議后,必須按照協議規定的內容來處理事情,協議中要求的方法必須實現(@optional的方法除外)。

protocol是一種語法,它提供了一個很方便的、實現delegate模式的機會。

定義protocol如下:

  • @protocol?ClassBDelegate<NSObject>?
  • -?(void)methodOne;?
  • @optional?
  • -?(void)methodTwo:(NSString?*)value;?
  • @end
  • 定義了一個ClassB的協議,這個協議中包含兩個方法,其中methodTwo為可選的。

    在ClassA的頭文件(ClassA.h)中實現這個協議,如下代碼:

  • @interface?ClassA<ClassBDelegate>?
  • @end
  • 在ClassA的實現文件(ClassA.m)中實現ClassBDelegate的兩個方法,其中methodTwo可以不實現,如下:

  • -?(void)methodOne{?
  • ????//?具體實現內容?
  • }?
  • ?
  • -?(void)methodTwo:(NSString?*)value{??
  • ????//?具體實現內容???
  • }
  • 2、代理(delegate),顧名思義就是委托別人辦事,當一件事情發生后,自己不處理,讓別人來處理。

    delegate和protocol沒有關系。delegate本身是一種設計模式。是把一個類自己需要做的一部分事情,讓另一個類(也可以就是自己本身)來完成。

    在ClassB的頭文件(ClassB.h)中定義一個代理如下:

  • @interface?ClassB?
  • @property?(nonatomic,?unsafe_unretained)?id<ClassBDelegate>?delegate;?
  • @end
  • 這樣,當我們在ClassB的實現文件(ClassB.m)中遇到想讓別的類(如 ClassA)處理的問題時,就可以這樣

  • [self.delegate?methodOne];?
  • [self.delegate?methodTwo:@"需要傳遞的值"];
  • 二 ?具體實例

    ? ?實現的功能是:在viewcontroller中創建一個UIButton按鈕用于push到下一個頁面,和一個UILable用于顯示從第二個頁面傳回的文字,在secondviewcontroller中創建一個UITextfield用于輸入文字。在輸入完成按下back返回第一個頁面后,在lable上顯示輸入的文字。

    1?工程截圖

    ?

    ? ?2 ?ViewController.h文件

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController

    @end

    ? 3 ?ViewController.m文件

    #import "ViewController.h"

    #import "SecondViewController.h"

    ?

    @interface ViewController ()<getTextFromDelegate>

    {

    ? ? UIButton *_button;

    ? ? UILabel *_lable;

    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {

    ? ? [super viewDidLoad];

    ? ? [self initLayout];

    }

    - (void)initLayout{

    ? ? _button = [UIButton buttonWithType:UIButtonTypeSystem];

    ? ? _button.frame = CGRectMake(0, 0, 100, 50);

    ? ? _button.center = self.view.center;

    ? ? _button.backgroundColor = [UIColor redColor];

    ? ? [_button addTarget:self action:@selector(pushToNextViewController:) forControlEvents:UIControlEventTouchUpInside];

    ? ? [_button setTitle:@"下一頁" forState:UIControlStateNormal];

    ? ? [self.view addSubview:_button];

    ?? ?

    ? ? _lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 64, 355, 200)];

    ? ? _lable.backgroundColor = [UIColor orangeColor];

    ? ? [self.view addSubview:_lable];

    }

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

    ? ? SecondViewController *secondVC = [SecondViewController new];

    ? ? //代理就是本身

    ? ? secondVC.delegate = self;

    ? ? [self.navigationController pushViewController:secondVC animated:YES];

    }

    #pragma mark 實現代理方法

    - (void)getText:(NSString *)text{

    ? ? _lable.text = text;

    }

    - (void)didReceiveMemoryWarning {

    ? ? [super didReceiveMemoryWarning];

    ? ? // Dispose of any resources that can be recreated.

    }

    ?@end

    4 ?SecondViewController.h文件

    #import <UIKit/UIKit.h>

    @protocol getTextFromDelegate <NSObject>

    //聲明協議方法

    - (void)getText:(NSString *)text;

    @end

    ?@interface SecondViewController : UIViewController

    ?@property (weak, nonatomic)id<getTextFromDelegate>delegate;

    @end

    4 ?SecondViewController.m文件

    #import "SecondViewController.h"

    @interface SecondViewController ()<getTextFromDelegate>

    {

    ? ? UITextField *_textField;

    }

    @end

    @implementation SecondViewController

    - (void)viewDidLoad {

    ? ? [super viewDidLoad];

    ? ? self.view.backgroundColor = [UIColor whiteColor];

    ? ? _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];

    ? ? _textField.backgroundColor = [UIColor redColor];

    ? ? _textField.center = self.view.center;

    ? ? [self.view addSubview:_textField];

    }

    #pragma mark 在頁面將要消失時

    - (void)viewWillDisappear:(BOOL)animated{

    ? ? //將本頁面獲取的值傳遞給上一個頁面去實現

    ? ? [self.delegate getText:_textField.text];

    }

    - (void)didReceiveMemoryWarning {

    ? ? [super didReceiveMemoryWarning];

    ? ? // Dispose of any resources that can be recreated.

    }

    @end

    三 ?模擬器運行結果截圖

    ?

    轉載于:https://www.cnblogs.com/blogoflzh/p/4676241.html

    總結

    以上是生活随笔為你收集整理的iOS传值之代理传值的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。