iOS学习笔记(7)视图控制器之间传递参数的三种方式
2015-07-02 20:31
561 查看
视图控制器之间传递参数的三种方式:1.委托(delegate) 2. block 3.通知(NSNotificationCenter)
1.委托
B视图切换到A视图并且携带参数
在B视图中制定协议,A视图来尊从协议
B视图
@protocol ModelDelegate <NSObject>
@optional
//可选,我们可以实现,也可以不实现
@required //必须要实现,默认@reqired
-(void)textFieldStr:(NSString*)str;
@end
@interface SecondViewController :
UIViewController<UITextFieldDelegate>
@property(nonatomic,assign)id<ModelDelegate> delegate;//代理对象
@end
===========
UITextField *textField=(UITextField*)[self.view
viewWithTag:12];
//判断delegate不为空并且我们协议中的方法实现
if (_delegate!=nil && [_delegate
respondsToSelector:@selector(textFieldStr:)]) {//如果不为空说明当前的代理对象就是第一个视图控制器对象
NSLog(@"//判断delegate不为空并且我们协议中的方法实现");
//我们的代理回传数据
[_delegate
textFieldStr:textField.text];
}
====================================
A视图
SecondViewController *second=[[SecondViewController
alloc]
init];
second.delegate=self;//将代理对象指向第一个视图控制器
//建立代理关系
[self
presentViewController:second animated:YES
completion:^{
}];
[second
release];
//协议中的方法
-(void)textFieldStr:(NSString *)str
{
UITextField *textField=(UITextField*)[self.view
viewWithTag:12];
textField.text=str;
}
==============================================
2.block
B视图切换到A视图,并且携带参数
那么B视图创建block
#import <UIKit/UIKit.h>
typedef
void(^TextFiledBlock)(NSString *);
@interface SecondViewController :
UIViewController
@property (nonatomic,copy)TextFiledBlock textfiledBlock;
@property (nonatomic,copy)void(^blockTest)(NSString *str);
@end
[self.navigationController
popViewControllerAnimated:YES];
UITextField *textField=(UITextField*)[self.view
viewWithTag:10];
if(self.textfiledBlock)
{
self.textfiledBlock(textField.text);
}
A视图接受参数
SecondViewController *second=[[SecondViewController
alloc]init];
UITextField *textField=(UITextField*)[self.view
viewWithTag:100];
second.textfiledBlock=^(NSString *str)
{
textField.text=str;
};
3.通知
B视图切换到A视图,并且携带参数
那么B视图是发送通知的一方
//创建我们的通知
-(void)createNoti
{
//第一个参数是通知用到名字
//第二个接收对象,nil群发
//第三个用户信息,我们要传的数据
UITextField *textField=(UITextField*)[self.view
viewWithTag:12];
NSDictionary *dict=[NSDictionary
dictionaryWithObjectsAndKeys:textField.text,@"textFieldStr",
nil];
//发送通知
[[NSNotificationCenter
defaultCenter] postNotificationName:@"SecondFieldStr"
object:nil
userInfo:dict];
}
A视图接受通知的一方
//用户通知中心
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(reciveFieldStr:)
name:@"SecondFieldStr"
object:nil];
//接收通知的方法
-(void)reciveFieldStr:(NSNotification*)notification
{
//NSLog(@"收到通知");
UITextField *textField=(UITextField*)[self.view
viewWithTag:12];
NSDictionary *dict=notification.userInfo;
textField.text=[dict
objectForKey:@"textFieldStr"];
}
1.委托
B视图切换到A视图并且携带参数
在B视图中制定协议,A视图来尊从协议
B视图
@protocol ModelDelegate <NSObject>
@optional
//可选,我们可以实现,也可以不实现
@required //必须要实现,默认@reqired
-(void)textFieldStr:(NSString*)str;
@end
@interface SecondViewController :
UIViewController<UITextFieldDelegate>
@property(nonatomic,assign)id<ModelDelegate> delegate;//代理对象
@end
===========
UITextField *textField=(UITextField*)[self.view
viewWithTag:12];
//判断delegate不为空并且我们协议中的方法实现
if (_delegate!=nil && [_delegate
respondsToSelector:@selector(textFieldStr:)]) {//如果不为空说明当前的代理对象就是第一个视图控制器对象
NSLog(@"//判断delegate不为空并且我们协议中的方法实现");
//我们的代理回传数据
[_delegate
textFieldStr:textField.text];
}
====================================
A视图
SecondViewController *second=[[SecondViewController
alloc]
init];
second.delegate=self;//将代理对象指向第一个视图控制器
//建立代理关系
[self
presentViewController:second animated:YES
completion:^{
}];
[second
release];
//协议中的方法
-(void)textFieldStr:(NSString *)str
{
UITextField *textField=(UITextField*)[self.view
viewWithTag:12];
textField.text=str;
}
==============================================
2.block
B视图切换到A视图,并且携带参数
那么B视图创建block
#import <UIKit/UIKit.h>
typedef
void(^TextFiledBlock)(NSString *);
@interface SecondViewController :
UIViewController
@property (nonatomic,copy)TextFiledBlock textfiledBlock;
@property (nonatomic,copy)void(^blockTest)(NSString *str);
@end
[self.navigationController
popViewControllerAnimated:YES];
UITextField *textField=(UITextField*)[self.view
viewWithTag:10];
if(self.textfiledBlock)
{
self.textfiledBlock(textField.text);
}
A视图接受参数
SecondViewController *second=[[SecondViewController
alloc]init];
UITextField *textField=(UITextField*)[self.view
viewWithTag:100];
second.textfiledBlock=^(NSString *str)
{
textField.text=str;
};
3.通知
B视图切换到A视图,并且携带参数
那么B视图是发送通知的一方
//创建我们的通知
-(void)createNoti
{
//第一个参数是通知用到名字
//第二个接收对象,nil群发
//第三个用户信息,我们要传的数据
UITextField *textField=(UITextField*)[self.view
viewWithTag:12];
NSDictionary *dict=[NSDictionary
dictionaryWithObjectsAndKeys:textField.text,@"textFieldStr",
nil];
//发送通知
[[NSNotificationCenter
defaultCenter] postNotificationName:@"SecondFieldStr"
object:nil
userInfo:dict];
}
A视图接受通知的一方
//用户通知中心
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(reciveFieldStr:)
name:@"SecondFieldStr"
object:nil];
//接收通知的方法
-(void)reciveFieldStr:(NSNotification*)notification
{
//NSLog(@"收到通知");
UITextField *textField=(UITextField*)[self.view
viewWithTag:12];
NSDictionary *dict=notification.userInfo;
textField.text=[dict
objectForKey:@"textFieldStr"];
}
相关文章推荐
- iOS学习笔记 (5)NSDate和NSTimer
- 使用LeanClound(AVOS)造成的ios linker command failed with exit code 1 (use -v to see invocation)
- IOS页面自动布局 之 NSLayoutConstraint基础篇
- iOS9适配系列教程
- iOS9适配系列教程
- iOS 详解CoreLocation定位服务
- IOS 百度地图一直报 manager start failed!
- IOS 8.1.3 GameCenter code15 沙盒无法登陆
- 解决Xcode升级导致插件不管用的问题
- iOS---设备的唯一标识详解
- 我为什么要学IOS?
- iOS8 高德地图SDK MAMapView无法定位的问题
- JS与iOS Native Code互调的优雅实现方案
- CocoaPods的安装和使用方法
- iOS开发多线程篇—NSOperation简单介绍
- IOS8解决获取位置坐标信息出错(Error Domain=kCLErrorDomain Code=0)(转)
- iOS 生产环境之实时日志,都打包了怎么查看报错信息
- 【转】storyboard入门
- 【转】在iOS 7中使用storyboard
- IOS LocationManager定位国内偏移,火星坐标(GCJ-02)解决方法