欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實(shí)現(xiàn)兩個(gè)控制器之間數(shù)據(jù)的雙向傳遞

 更新時(shí)間:2016年05月25日 10:32:05   作者:xiaojinwy  
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)兩個(gè)控制器之間數(shù)據(jù)的雙向傳遞的相關(guān)資料,感興趣的小伙伴們可以參考一下

本文為大家分享了iOS控制器之間數(shù)據(jù)的雙向傳遞,供大家參考,具體內(nèi)容如下

首先,有兩個(gè)控制器,分別為控制器A、控制器B。
A->B:數(shù)據(jù)由控制器A傳向控制器B,這叫做數(shù)據(jù)的順傳;數(shù)據(jù)由控制器B傳向控制器A,這叫做逆?zhèn)鳌?
順傳:一般通過(guò)創(chuàng)建目標(biāo)控制器對(duì)象,將數(shù)據(jù)賦值給對(duì)象的成員來(lái)完成;
逆?zhèn)鳎?/strong>一般使用代理來(lái)實(shí)現(xiàn),其中控制器A是控制器B的代理(控制器A監(jiān)聽(tīng)控制器B,控制器B通知控制器A)。
下面是博主寫(xiě)的簡(jiǎn)單實(shí)現(xiàn)了兩個(gè)控制間實(shí)現(xiàn)數(shù)據(jù)的雙向傳遞的app的demo:
1、這是界面設(shè)計(jì):

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()<SecondViewControllerDelegate>

/** 用于寫(xiě)入數(shù)據(jù),最后該數(shù)據(jù)用于傳遞給第二個(gè)界面 */
@property (weak, nonatomic) IBOutlet UITextField *first2Second;
/** 用于顯示第二個(gè)界面返回來(lái)時(shí)傳遞的數(shù)據(jù) */
@property (weak, nonatomic) IBOutlet UITextField *displayWithSecond;

@end

@implementation FirstViewController

- (void)viewDidLoad {
  [super viewDidLoad];

}

#pragma mark - Navigation

//點(diǎn)擊傳遞按鈕時(shí)會(huì)自動(dòng)調(diào)用此方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  SecondViewController *vc = (SecondViewController *)segue.destinationViewController;
  if (self.first2Second.text.length > 0) {
    //將該界面中的數(shù)據(jù)傳遞給第二個(gè)界面
    vc.name = self.first2Second.text;
  }

  //設(shè)置當(dāng)前控制器為SecondViewController控制器的代理
  vc.delegate = self;
}

#pragma mark - 實(shí)現(xiàn)SecondViewControllerDelegate中的協(xié)議方法
-(void)secondViewControllerDidDit:(SecondViewController *)viewController andName:(NSString *)name
{
  //將第二個(gè)界面中的數(shù)據(jù)返回給第一個(gè)界面(此界面)
  self.displayWithSecond.text = name;
}

@end

SecondViewController.h

#import <UIKit/UIKit.h>
@class SecondViewController;

@protocol SecondViewControllerDelegate <NSObject>

/** SecondViewControllerDelegate協(xié)議中的方法 */
-(void)secondViewControllerDidDit:(SecondViewController *)viewController andName:(NSString *)name;

@end

@interface SecondViewController : UIViewController

@property(nonatomic,strong) NSString *name;
@property(nonatomic,weak) id<SecondViewControllerDelegate> delegate;

@end



SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()
/** 用于寫(xiě)入數(shù)據(jù),最后將數(shù)據(jù)返回給第一個(gè)界面 */
@property (weak, nonatomic) IBOutlet UITextField *second2First;
/** 用于顯示第一個(gè)界面?zhèn)鬟^(guò)來(lái)的數(shù)據(jù) */
@property (weak, nonatomic) IBOutlet UITextField *displayWithFirst;
/** 點(diǎn)擊此按鈕,第二個(gè)控制器將彈出棧,界面將返回到第一個(gè)界面 */
- (IBAction)second2First:(UIButton *)sender;

@end

@implementation SecondViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  //顯示第一個(gè)界面?zhèn)鬟f過(guò)來(lái)的數(shù)據(jù)信息
  self.displayWithFirst.text = self.name;

}

//點(diǎn)擊該按鈕,數(shù)據(jù)將返回給第一個(gè)界面顯示
- (IBAction)second2First:(UIButton *)sender {
  if (self.second2First.text.length > 0) {
    //如果有實(shí)現(xiàn)該協(xié)議方法的控制器,則將數(shù)據(jù)傳給該控制器
    if ([self.delegate respondsToSelector:@selector(secondViewControllerDidDit:andName:)]) {
      [self.delegate secondViewControllerDidDit:self andName:self.second2First.text];
    }
  }
  [self.navigationController popViewControllerAnimated:YES];
}

@end

以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論