Objective-C界面传值(二):协议传值

协议传值

协议传值是利用协议的特性来实现界面传值的一种方法.
我们把要传值得页面当作协议声明者,把接收值得页面作为代理人
让代理人执行协议方法,从而将值传到接收页面

下面我们来看一下协议传值的具体步骤:

首先要明白签订协议的完整步骤:
1.声明协议
2.声明代理人属性
3.让代理人执行协议方法
4.签订协议
5.指定代理人
6.实现协议方法

下面来看一下代码实现:

AppDelegate.m的实现过程在这里不在重复给出,具体步骤可以到我的上一篇文章里阅读:(Objective-C界面传值(一):属性传值)

ViewController.m

#import "ViewController.h"
#import "SecondVIewController.h"
#pragma mark 4.签协议
@interface ViewController ()

@property(nonatomic, retain) UITextField *textField;

@end

@implementation ViewController

- (void)dealloc
{
    [_textField release];
    [super dealloc];
}

- (void)loadView
{
    [super loadView];

    self.textField             = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 50)];
    self.textField.placeholder = @"请输入文本";
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textField];
    [_textField release];

    UIBarButtonItem *rightButton           = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(didClickedRightBarButton:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    [rightButton release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"首页";
    self.navigationController.navigationBar.barStyle    = UIBarStyleBlack;
    self.navigationController.navigationBar.translucent = NO;
}

#pragma mark 按钮点击方法
- (void)didClickedRightBarButton:(UIBarButtonItem *)button
{
    SecondVIewController *vc2 = [[SecondVIewController alloc] init];
#pragma mark 5.设置代理人
    vc2.delegate              = self;
    //从第一页向第二页传值依然采用属性传值
    vc2.secondString          = self.textField.text;
    [self.navigationController pushViewController:vc2 animated:YES];
    [vc2 release];
}

#pragma mark 6.实现协议方法
- (void)passString:(NSString *)string
{
    self.textField.text = string;
}

SecondViewController.h

#import 

#pragma mark 1.声明协议
@protocol PassValueProtocol 

- (void)passString:(NSString *)string;

@end

@interface SecondVIewController : UIViewController
/**
 *  属性传值字符串
 */
@property(nonatomic, copy) NSString *secondString;
#pragma mark 2.声明代理人属性
@property(nonatomic, assign) id delegate;

@end

SecondViewController.m

#import "SecondVIewController.h"

@interface SecondVIewController ()

@property(nonatomic, retain) UITextField *textField;

@end

@implementation SecondVIewController

- (void)dealloc
{
    [super dealloc];
}

- (void)loadView
{
    [super loadView];

    self.textField             = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 50)];
    self.textField.placeholder = @"请输入文本";
    self.textField.text        = self.secondString;
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textField];
    [_textField release];

    UIBarButtonItem *leftButton           = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(didClickedLeftBarButton:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    [leftButton release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"第二页";
}

#pragma mark 按钮点击方法
- (void)didClickedLeftBarButton:(UIBarButtonItem *)button
{
#pragma mark 3.代理人执行协议方法
    [self.delegate passString:self.textField.text];
    [self.navigationController popViewControllerAnimated:YES];
}

需要注意的几点:

  • 1.协议的6步一定不能缺少
  • 2.代码块中只是写了需要用到的几个方法的内容,系统自动生成的方法请不要随便删除
  • 3.控件设计坐标是根据iPhone6或iPhone6s尺寸写的,并没有做适配.

效果图:

Objective-C界面传值(二):协议传值_第1张图片
首页框中输入字符串,点击"Add"

Objective-C界面传值(二):协议传值_第2张图片
属性传值成功

Objective-C界面传值(二):协议传值_第3张图片
在第二页文本框中输入字符串,点击"Cancel"

Objective-C界面传值(二):协议传值_第4张图片
协议传值成功

总结,协议传值的用法步骤相对较多,需要勤加记忆.本文中实现的只是简单功能.如果有错误可以联系我,我会及时核对并改正!

你可能感兴趣的:(Objective-C界面传值(二):协议传值)