iOS属性传值、代理传值

import "AppDelegate.h"

import "FirstViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    FirstViewController *firstVC = [[FirstViewController alloc]init];
    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:firstVC];
    [self.window setRootViewController:navC];
    return YES;
    }


import "FirstViewController.h"

import "SecondViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // 设置标题
    self.navigationItem.title = @"FirstVC";
    // 设置导航条右侧按钮
    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightBtnItem;

    // 设置导航左侧按钮(点击后回收键盘)
    UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"回收键盘" style:UIBarButtonItemStyleDone target:self action:@selector(endEditingAction)];
    leftBtnItem.tag = 2000;
    self.navigationItem.leftBarButtonItem = leftBtnItem;

    // 初始化一个textField文本输入框
    UITextField *userTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
    userTextField.borderStyle = UITextBorderStyleBezel;
    userTextField.textAlignment = NSTextAlignmentCenter;
    userTextField.placeholder = @"请输入文字";
    userTextField.tag = 1000;
    [self.view addSubview:userTextField];

    UITextField *pwsTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 180, 200, 40)];
    pwsTextField.borderStyle = UITextBorderStyleBezel;
    pwsTextField.textAlignment = NSTextAlignmentCenter;
    pwsTextField.placeholder = @"密 码";
    pwsTextField.keyboardType = UIKeyboardTypeNumberPad;
    pwsTextField.secureTextEntry = YES;
    pwsTextField.tag = 1001;
    [self.view addSubview:pwsTextField];

    UITextField *yanZhengTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 260, 200, 40)];
    

    yanZhengTextField.borderStyle = UITextBorderStyleBezel;
    yanZhengTextField.textAlignment = NSTextAlignmentCenter;
    yanZhengTextField.keyboardType = UIKeyboardTypeNumberPad;
    pwsTextField.keyboardType = UIKeyboardTypeNumberPad;
    yanZhengTextField.placeholder = @"请输入验证码";
    yanZhengTextField.tag = 1002;
    [self.view addSubview:yanZhengTextField];

    }
    // 导航条右侧按钮回调方法,具体实现为推送到下个界面
    -(void)rightBarButtonAction:(UIBarButtonItem*)sender{
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    // 将文本输入框的值赋值secondVC的属性
    UITextField userTextField = (UITextField)[self.view viewWithTag:1000];// textField为局部变量
    secondVC.userStr = userTextField.text;
    UITextField pwsTextField = (UITextField)[self.view viewWithTag:1001];
    secondVC.pwsStr = pwsTextField.text;
    UITextField yanZhengTextField = (UITextField)[self.view viewWithTag:1002];
    secondVC.yanZhengStr = yanZhengTextField.text;

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

// 导航条左侧按钮回调方法
-(void)endEditingAction{
[self.view endEditing:YES];
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (nonatomic ,retain)NSString *userStr; // 用来接受第一个界面传过来的内容
@property (nonatomic ,retain)NSString *pwsStr;
@property (nonatomic ,retain)NSString *yanZhengStr;

@end

import "SecondViewController.h"

import "ThirdViewController.h"

@interface SecondViewController ()<ThirdViewControllerDelegate>

@end

@implementation SecondViewController

// thirdVC的代理方法,作用为,将thirdVC上的值传递到当前的视图控制器
-(void)passValueWithDic:(NSDictionary *)valueDic{
NSLog(@"dic_______%@",valueDic);
UILabel userLabel = (UILabel)[self.view viewWithTag:5000];
userLabel.text = [valueDic valueForKey:@"name"];

UILabel *pswLabel = (UILabel*)[self.view viewWithTag:5001];
pswLabel.text = [valueDic valueForKey:@"pws"];

UILabel *yanZhengLabel = [self.view viewWithTag:5002];
yanZhengLabel.text = [valueDic valueForKey:@"yanZheng"];

}

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"SecondVC";
    // 声明一个label,用来显示首个界面传过来的内容
    UILabel *userLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
    // label显示的内容为从首个界面传过来的值
    userLabel.text = self.userStr;
    userLabel.tag = 5000;
    [self.view addSubview:userLabel];

    UILabel *pwsLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 180, 200, 40)];
    pwsLabel.text = self.pwsStr;
    pwsLabel.tag = 5001;
    [self.view addSubview:pwsLabel];

    UILabel *yanZhengLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 260, 200, 40)];
    yanZhengLabel.text = self.yanZhengStr;
    yanZhengLabel.tag = 5002;
    [self.view addSubview:yanZhengLabel];

    // 设置导航条右侧按钮,点击跳转到下个界面(ThirdViewController)
    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightBtnItem;
    }

// 导航条右侧按钮回调方法,点击跳转到下个界面(ThirdViewController)
-(void)rightBarButtonAction:(UIBarButtonItem*)sender{
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
// 指定代理 {self:在类方法(加号方法)中指的是本类,在对象方法(减号方法)中指的是该类的一个对象}
thirdVC.delegate = self;
[self.navigationController pushViewController:thirdVC animated:YES];
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    @end

import <UIKit/UIKit.h>

@protocol ThirdViewControllerDelegate <NSObject>

// 该方法的参数就是要传递的值
-(void)passValueWithDic:(NSDictionary*)valueDic;

@end

@interface ThirdViewController : UIViewController

@property (nonatomic ,assign)id<ThirdViewControllerDelegate> delegate;

@end

import "ThirdViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"ThirdVC";

    for (int i = 0; i < 3; i++) {
    UITextField textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100+100i, 200, 40)];
    textField.borderStyle = UITextBorderStyleBezel;
    textField.textAlignment = NSTextAlignmentCenter;
    textField.placeholder = @"请输入文字";
    textField.tag = 1000+i;
    [self.view addSubview:textField];
    }

    // 因为需要向前传值,所以需要捕获点击按钮的事件,所以需要重新定义leftBarButtonItem
    UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(backBtnAction:)];
    leftBtnItem.tag = 2000;
    self.navigationItem.leftBarButtonItem = leftBtnItem;

}
// 返回按钮的点击事件
-(void)backBtnAction:(UIBarButtonItem*)sender{
UITextField nameTextField = (UITextField)[self.view viewWithTag:1000];
UITextField pwsTextField = (UITextField)[self.view viewWithTag:1001];
UITextField yanZhengTextField = (UITextField)[self.view viewWithTag:1002];

/*
 if条件判断中
 第一步:先判断是否指定了代理
 第二步:respondsToSelector该方法的返回值为BOOL类型。该方法会从指定的代理类中(这里我们的代理类就是SecondViewController)找寻方法选择器重中方法的实现,如果没有实现该协议方法,就返回NO,如果实现,就返回YES
 self.delegate : 指定哪个类为代理,它就是代理类的一个对象,在这里它指的就是 SecondViewController这个类的一个对象那,我们在SecondViewController也实现了passValueWithDic:这个方法,所以我们可以调用此方法
 (self.delegate = SecondViewController)
      */

if (self.delegate && [self.delegate respondsToSelector:@selector(passValueWithDic:)]) {
[self.delegate passValueWithDic:@{@"name":nameTextField.text,@"pws":pwsTextField.text,@"yanZheng":yanZhengTextField.text}];
}

[self.navigationController popViewControllerAnimated:YES];

}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    @end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 1,484评论 2 4
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 762评论 0 0
  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,558评论 2 7
  • 第一次在简书写东西,表示有点紧张啊。我就想把简书当作可以倾述的地方,可以写文章的地方。我现在常常思考我们如何可以勇...
    你不应该太帅啊阅读 180评论 0 0
  • --著:江涵小子 丁环小宝 鸟亦群飞更亦人, 今京君心为一人; 齐心同德创嘉业, 比翼双飞又是春。
    江涵少年阅读 98评论 0 3