協議傳值
協議傳值是利用協議的特性來實現界面傳值的一種方法.
我們把要傳值得頁面當作協議聲明者,把接收值得頁面作為代理人
讓代理人執行協議方法,從而將值傳到接收頁面
下面我們來看一下協議傳值的具體步驟:
首先要明白簽訂協議的完整步驟:
1.聲明協議
2.聲明代理人屬性
3.讓代理人執行協議方法
4.簽訂協議
5.指定代理人
6.實現協議方法
下面來看一下代碼實現:
AppDelegate.m的實現過程在這里不在重復給出,具體步驟可以到我的上一篇文章里閱讀:(Objective-C界面傳值(一):屬性傳值)
ViewController.m
#import "ViewController.h"
#import "SecondVIewController.h"
#pragma mark 4.簽協議
@interface ViewController ()<PassValueProtocol>
@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 <UIKit/UIKit.h>
#pragma mark 1.聲明協議
@protocol PassValueProtocol <NSObject>
- (void)passString:(NSString *)string;
@end
@interface SecondVIewController : UIViewController
/**
* 屬性傳值字符串
*/
@property(nonatomic, copy) NSString *secondString;
#pragma mark 2.聲明代理人屬性
@property(nonatomic, assign) id<PassValueProtocol> 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尺寸寫的,并沒有做適配.
效果圖:
首頁框中輸入字符串,點擊"Add"
屬性傳值成功
在第二頁文本框中輸入字符串,點擊"Cancel"
協議傳值成功
總結,協議傳值的用法步驟相對較多,需要勤加記憶.本文中實現的只是簡單功能.如果有錯誤可以聯系我,我會及時核對并改正!