iOS 页面之间传参的总结
来源:互联网 发布:北京凶宅数据库 编辑:程序博客网 时间:2024/05/16 06:42
属性 从 a到b b不能到 a
代理
代码块
单例
通知
属性传参 a为父视图 b为子视图
1.属性
在b的.h文件中设置对应的接收属性 NSString*recText;
UITextField *receiveText;
在 a中对属性赋值
TwoViewController*twov=[TwoViewControllernew];
[self.navigationControllerpushViewController:twovanimated:YES];
twov.recText=self.userText.text;
在b中 接收传输的属性
[self.receiveTextsetText:self.recText];//图片类似
2.代理
在传送方写协议 接收方遵守协议
b a
b的h文件中写
@protocol Mypro<NSObject>
-(void)setTextValue:(NSString *)text;
@end
@property(assign,nonatomic)id<Mypro>delegate;//设置代理属性
a中
@interface OneViewController()<Mypro>//遵守协议
-(void)setTextValue:(NSString *)text//代理传参
{
self.userText.text=text;
}
TwoViewController *twov=[TwoViewControllernew];
twov.delegate=self;//指明协议遵守者为a
b中
[self.delegatesetTextValue:self.receiveText.text];//代理传参
3.代码块传参
a b
在b的.h中定义
typedefvoid(^setupTitle) (NSString*);
@interface NcViewController : UIViewController
@property(copy,nonatomic)setupTitlemytitle;
@end//定义出代码块1
在b.m中赋值
self.mytitle(self.userText.text);//给代码块的参数赋值2
在a中调用
twoc.mytitle=^(NSString *xc)
{
self.title=xc;
};//正式调用代码块
4.单例
//1.定义单例对象 static类型变量
staticSing * instance;
//2.单例对象创建方法
+(id)getinstance
{ @synchronized(self)//多线程同步,防止多线程的并发访问
{
if (instance==nil)
{
instance=[[Singalloc]init];//创建新对象
}
}
returninstance;
}
//重写方法 防止用户通过alloc方法构建多个对象
+(instancetype)allocWithZone:(struct_NSZone *)zone
{
if (instance==nil) {
instance=[[superallocWithZone:zone]init];
}
returninstance;
}
-(void)pass:(id)sender//单例传参方法
{
Sing *obj=[Singgetinstance];//创建单例对象obj
obj.titleStr=self.usertext.text;//
}
接收方
-(void)viewDidAppear:(BOOL)animated//每次视图要显示的时候
{
self.title=[[Singgetinstance]titleStr];
}
5.通知传参
//1注册通知
[[NSNotificationCenterdefaultCenter]addObserver:twocselector:@selector(changeBadgeValue:)name:@"w的通知"object:nil];
// 2发送通知
[[NSNotificationCenterdefaultCenter]postNotificationName:@"w的通知"object:nil];
- iOS 页面之间传参的总结
- iOS 页面之间的传值总结
- iOS 页面之间的传值总结
- iOS 页面之间的传值总结
- iOS 页面之间的传值
- iOS 页面之间的传值方式
- ios页面之间传值
- iOS页面之间传值
- iOS delegate 实现页面之间的传值
- iOS delegate 实现页面之间的传值
- iOS 用delegate方式实现页面之间的传值
- iOS delegate 实现页面之间的传值
- iOS页面之间传值-----delegate的运用
- IOS 页面之间的跳转以及传值
- IOS-block实现页面之间的传值
- iOS - 页面之间传值的实现 - block
- iOS - 页面之间传值的实现 - 代理
- 关于jsp页面之间传值的中文乱码总结
- AIDL通讯注意事项
- 获取主要变例
- [bzoj4556][TJOI&&HEOI2016]字符串
- Ubuntu下管理启动服务
- 图像绘制到屏幕上的原理。
- iOS 页面之间传参的总结
- ubuntu下使用apt方式安装、卸载nginx
- Linux中的文件类型
- 最小-最大搜索
- Jsp与servlet的区别
- Java面试题及详解
- CutOut 4破解心得
- android界面设计的几个良好习惯
- idm未完成的想法