iOS 多视图—视图切换之代理Delegate传参切换
2016-03-15 19:38
531 查看
步骤:先在iOS平台创建一个工程 ,之后再工程中创建有类(FirstVIewController)
代码实现如下
FirstViewController.h
#import <UIKit/UIKit.h>
//创建协议 声明协议
@protocol PostVslueDelegate <NSObject>
-(void)postValue:(NSString *)info;
@end
@interface FirstViewController : UIViewController<UITextFieldDelegate>
//声明属性
@property(strong,nonatomic)UITextField *textName;
@property(strong,nonatomic)NSString *string;
@property(strong,nonatomic)id<PostVslueDelegate> Delegate;
@end
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置主屏背景色
self.view.backgroundColor=[UIColor yellowColor];
//创建文本框
self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 40)];
self.textName.borderStyle=1;
//指定代理
self.textName.delegate=self;
//传值
self.textName.text=self.string;
[self.view addSubview:self.textName];
}
//实现代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//调用代理方法
if (self.Delegate) {
[self.Delegate postValue:textField.text];
}
//切换页面
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"切换成功");
}];
//隐藏键盘
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface ViewController : UIViewController<UITextFieldDelegate,PostVslueDelegate>
@property(strong,nonatomic)UITextField *textName;
@property(strong,nonatomic)NSString *string;
@property(strong,nonatomic)UIButton *myButton;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//背景色
self.view.backgroundColor=[UIColor greenColor];
//创建文本框
self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 50)];
self.textName.borderStyle=1;
//指定代理
self.textName.delegate=self;
[self.view addSubview:self.textName];
//创建按钮
self.myButton=[[UIButton alloc]initWithFrame:CGRectMake(150, 160, 50, 50)];
self.myButton.backgroundColor=[UIColor redColor];
[self.myButton setTitle:@"Next" forState:UIControlStateNormal];
[self.myButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.myButton];
}
-(void)nextPage
{
FirstViewController *firstVC=[[FirstViewController alloc]init];
//正传参值
firstVC.string=self.textName.text;
//指定代理
firstVC.Delegate=self;
//切换视图
[self presentViewController:firstVC animated:YES completion:^{
NSLog(@"切换成功");
}];
}
//协议方法
-(void)postValue:(NSString *)info
{
//功能:反传参值
self.textName.text=info;
}
//代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//隐藏键盘
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果图
1、从第一个视图传参(The only)到下一个视图(正向传参)
![](cid:d5f18784d98a865612eeb65fe8ebe6a9)
![](cid:813b475da372468b4f21fe70c7a135ee)
1、从视图传参(The only one of)到上一个视图(逆向传参)
![](cid:a18e5ab499c13d2bd52e2853367b01a1)
代码实现如下
FirstViewController.h
#import <UIKit/UIKit.h>
//创建协议 声明协议
@protocol PostVslueDelegate <NSObject>
-(void)postValue:(NSString *)info;
@end
@interface FirstViewController : UIViewController<UITextFieldDelegate>
//声明属性
@property(strong,nonatomic)UITextField *textName;
@property(strong,nonatomic)NSString *string;
@property(strong,nonatomic)id<PostVslueDelegate> Delegate;
@end
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置主屏背景色
self.view.backgroundColor=[UIColor yellowColor];
//创建文本框
self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 40)];
self.textName.borderStyle=1;
//指定代理
self.textName.delegate=self;
//传值
self.textName.text=self.string;
[self.view addSubview:self.textName];
}
//实现代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//调用代理方法
if (self.Delegate) {
[self.Delegate postValue:textField.text];
}
//切换页面
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"切换成功");
}];
//隐藏键盘
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface ViewController : UIViewController<UITextFieldDelegate,PostVslueDelegate>
@property(strong,nonatomic)UITextField *textName;
@property(strong,nonatomic)NSString *string;
@property(strong,nonatomic)UIButton *myButton;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//背景色
self.view.backgroundColor=[UIColor greenColor];
//创建文本框
self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 50)];
self.textName.borderStyle=1;
//指定代理
self.textName.delegate=self;
[self.view addSubview:self.textName];
//创建按钮
self.myButton=[[UIButton alloc]initWithFrame:CGRectMake(150, 160, 50, 50)];
self.myButton.backgroundColor=[UIColor redColor];
[self.myButton setTitle:@"Next" forState:UIControlStateNormal];
[self.myButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.myButton];
}
-(void)nextPage
{
FirstViewController *firstVC=[[FirstViewController alloc]init];
//正传参值
firstVC.string=self.textName.text;
//指定代理
firstVC.Delegate=self;
//切换视图
[self presentViewController:firstVC animated:YES completion:^{
NSLog(@"切换成功");
}];
}
//协议方法
-(void)postValue:(NSString *)info
{
//功能:反传参值
self.textName.text=info;
}
//代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//隐藏键盘
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果图
1、从第一个视图传参(The only)到下一个视图(正向传参)
1、从视图传参(The only one of)到上一个视图(逆向传参)
相关文章推荐
- iOS小技巧17-iPhone如何安装ipa包
- iOS开发技术之人脸识别
- iOS8Extension之Today插件
- IOS NSThread
- iOS优化
- 【ios】相册、定位等权限判断
- 自制大体模仿ios 四位密码输入界面
- iOS-html调起APP并传参
- iOS多页面传值方式之单例传值singleton
- iOS开发中的火星坐标系及各种坐标系转换算法
- IOS的KVO不能监听的场景
- iOS本地推送
- iOS下KVO使用过程中的陷阱
- iOS倒计时
- iOS 高德地图(二)(进阶具体使用的细节)
- iOS 高德地图(-)(基础篇)
- iOS- CALayer绘图,如何绘制渐变效果图
- iOS属性传值
- ios实现条形码扫描功能
- iOS下KVO使用过程中的陷阱